Android:相对布局综合小演练—智能家居,按键快速美化的小技巧
????????一、相對布局綜合小演練—智能家居
? ??
需要用到的圖片
新建一個工程
?
?首先,里面的?????????????????android:paddingBottom="@dimen/activity_vertical_margin"
? ? ????????????????????????????????????????android:paddingLeft="@dimen/activity_horizontal_margin"
? ????????????????????????????????????????? android:paddingRight="@dimen/activity_horizontal_margin"
? ????????????????????????????????????????? android:paddingTop="@dimen/activity_vertical_margin"
不需要
?然后那幾張圖片放在? ? ?res? ?的? ? drawable 下面
? ? ? ??
?然后給最大的圖片當背景
運行一下 然后圖就進來了
?去掉hello? world,
然后把它? ? pic_rf? ? ?放在父親的正中央
?運行一下
?
在加上一張圖片
運行一下
?
?修改一下,用左面的內邊距,用到padding
?運行一下
然后再來一個button?
?運行一下
我想要? ? “ 刷卡”在正中央,然后跟底部對齊
?
?運行一下
?
?然后上面再來一個小布局
運行一下
?
看一下現在的布局
?
想要在里面添加控件?
?運行一下,左面太頂,上面也太頂
修改一下
?
?
運行一下
?
?
?再來兩個button
運行一下
?修改一下代碼
?運行一下
?現在完整的代碼
?
?
?運行一下
?
二、按鍵快速美化的小技巧
我們的? ? ? “刷卡”? ? 這個按鍵有點土,我能不能給他優化一下做個好看的背景
比如說找一個底圖
?運行一下
這是一種方式,
但是別的按鍵呢,他們按完是有反應的
?
參考文獻? ? ? ??Android 圓角按鈕的實現_tracy的博客-CSDN博客_android button 圓角Android 圓角按鈕的實現_tracy的博客-CSDN博客_android button 圓角Android 圓角按鈕的實現效果圖:在res/drawable目錄下新建按鈕樣式文件 btn_normal.xml(正常狀態) 和 btn_pressed.xml(按下狀態)。btn_normal.xml文件:<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schema...https://blog.csdn.net/tracydragonlxy/article/details/88552262Android 圓角按鈕的實現_tracy的博客-CSDN博客_android button 圓角
? ? ?
Android 圓角按鈕的實現
效果圖:????????
?
1、在res/drawable目錄下新建按鈕樣式文件?btn_normal.xml(正常狀態) 和?btn_pressed.xml(按下狀態)。
btn_normal.xml文件:
????????<?xml version="1.0" encoding="utf-8"?>
????????<shape
????????? ? xmlns:android="http://schemas.android.com/apk/res/android"
????????? ? android:shape="rectangle">
????????????????? ? <!-- 圓角的半徑 -->
????????? ? <corners android:radius="10dp"/>
????????????????? ? <!-- 填充顏色 -->
? ????????? <solid android:color="#3a8fea"/>
????????</shape>
? ? ? ??
? ? ? ? ? ?
? ? ?btn_pressed.xml文件:
????????????????<?xml version="1.0" encoding="utf-8"?>
????????????????<shape?
?????????????????? ?xmlns:android="http://schemas.android.com/apk/res/android"
?????????????????? ?android:shape="rectangle">
????????????????????????? ? <!-- 圓角的半徑 -->
????????????????? ? <corners android:radius="10dp"/>
????????????????????????? ? <!-- 填充顏色 -->
????????????????? ? <solid android:color="#0662f5"/>
????????????????????????</shape>
? ? ? ?
? ? 2、在res/drawable目錄下新建樣式文件?btn_selector.xml?文件,定義按鈕的不同狀態樣式。
????????????????btn_selector.xml文件:
????????????????<?xml version="1.0" encoding="utf-8"?>
????????<selector xmlns:android="http://schemas.android.com/apk/res/android">
? ????????????????????????? <!-- 正常狀態 -->
?????????? ?<item android:drawable="@drawable/btn_normal" android:state_pressed="false"/>
????????????????????????? ? <!-- 按下狀態 -->
????????? ? <item android:drawable="@drawable/btn_pressed" android:state_pressed="true"/>
????????</selector>
? ? ? ? ? ? ? ? ? ? ? ? 3、使用按鈕樣式。
????????????????????????activity_button.xml文件:
????????????????????????
????????<?xml version="1.0" encoding="utf-8"?>
????????<android.support.constraint.ConstraintLayout
????????? ? xmlns:android="http://schemas.android.com/apk/res/android"
????????? ? xmlns:app="http://schemas.android.com/apk/res-auto"
????????? ? xmlns:tools="http://schemas.android.com/tools"
????????? ? android:layout_width="match_parent"
????????? ? android:layout_height="match_parent"
????????????????? ? tools:context=".ButtonActivity">
????????? ? <Button
? ? ????????? ? android:id="@+id/btn1"
? ? ????????? ? android:layout_width="wrap_content"
? ? ????????? ? android:layout_height="wrap_content"
? ? ????????? ? android:layout_marginTop="30dp"
? ? ? ????????? android:layout_marginStart="30dp"
? ???????????? ? android:text="button"
? ? ????????? ? android:textColor="#fff"
? ????????? ? ? android:background="@drawable/btn_selector"
? ? ? ????????? app:layout_constraintLeft_toLeftOf="parent"
? ? ? ????????? app:layout_constraintTop_toTopOf="parent" />
????????</android.support.constraint.ConstraintLayout>
·android:background="@drawable/btn_selector"
其中的btn_selector是我們自定義的xml樣式文件。
????????
?運行結果圖:
4、 給圓角按鈕加上虛線邊框樣式。????????????????
????????????????btn_normal.xml文件:
????????
????????<?xml version="1.0" encoding="utf-8"?>
????????<shape
????????? ? xmlns:android="http://schemas.android.com/apk/res/android"
????????? ? android:shape="rectangle">
????????????????? ????????? <!-- 圓角的半徑 -->
????????? ? <corners android:radius="10dp"/>
? ? ????????????????????????<!-- 填充顏色 -->
????????? ? <solid android:color="#3a8fea"/>
????????? ????????????????? <!-- 邊框的寬度,每段虛線的長度,和兩段虛線之間的間隔和顏色 -->
????????? ? <stroke
????????? ? ? ? android:width="2dp"
? ????????? ? ? android:dashWidth="6dp"
? ? ????????? ? android:dashGap="6dp"
? ? ? ????????? android:color="#e75050" />
????????????????</shape>
? ? ??
? ?運行結果圖:
????????????????
?如果希望按鈕邊框是實線,那么把dashWidth和dashGap屬性去除即可。
?實線邊框樣式:
????????<?xml version="1.0" encoding="utf-8"?>
????????????????<shape
????????? ? xmlns:android="http://schemas.android.com/apk/res/android"
????????? ? android:shape="rectangle">
? ????????????????????????? <!-- 圓角的半徑 -->
? ????????? <corners android:radius="10dp"/>
? ????????????????????????? <!-- 填充顏色 -->
? ????????? <solid android:color="#3a8fea"/>
????????????????????????? ? <!-- 實線邊框 -->
????????????????? ? <stroke
? ? ????????? ? android:width="2dp"
? ? ????????? ? android:color="#e75050" />
????????</shape>
運行結果圖
:
?
? ? ?
? ?5、實現局部圓角樣式。
????????<?xml version="1.0" encoding="utf-8"?>
????????<shape
????????? ? xmlns:android="http://schemas.android.com/apk/res/android"
? ????????? android:shape="rectangle">
? ????????????????????????? <!-- 圓角的半徑,左上/右下實現圓角 -->
? ????????? <corners
? ? ????????? ? android:topLeftRadius="10dp"
? ? ? ????????? android:bottomRightRadius="10dp"/>
? ????????????????????????? <!-- 填充顏色 -->
? ????????? <solid android:color="#3a8fea"/>
????????</shape>
? ??運行結果圖:
????????????????
????????
?
????????????????
????????????????<gradient
????????? android:angle="integer"
????????? android:centerX="Float"
????????? android:centerY="Float"
????????? android:centerColor="integer"
????????? android:startColor="color"
????????? android:endColor="color"
????????? android:gradientRadius="integer"
????????? android:type=["linear"|"radial"|"sweep"]
????????? android:usesLevel=["true"|"false"]
????????? />
? ? ?angle:角度,當 android:type=“linear”時有效 ,以45度為單位,逆時針方向旋轉
centerX:Float。漸變色中心的 X 相對位置( 0-1.0 )。當 android:type=“linear”時無效
centerY:Float。漸變色中心的 Y 相對位置( 0-1.0 )。當 android:type=“linear”時無效
centerColor:color。可選的顏色,出現在 start 和 end 顏色之間。
gradientRadius:Float。漸變色的半徑。當 android:type=“radial” 時有效。
startcolor:開始的顏色
endcolor:結束的顏色
type:漸變色的樣式。有效值為:
“linear”:線性漸變,默認值
“radial”:環形漸變。 start 顏色是處于中間的顏色
“sweep”:扇形漸變
useLevel:Boolean。“ true ”表示可以當作 LevelListDrawable 使用
?
?
<?xml version="1.0" encoding="utf-8"?>
<shape
? ? xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:shape="rectangle">
? ????????????????? <!-- 圓角的半徑 -->
? ? <corners android:radius="10dp"/>
? ? ????????????????<!-- 填充顏色 -->
? ? <solid android:color="#3a8fea"/>
?? ?
? ? <gradient
? ? ? ? android:angle="180"
? ? ? ? android:startColor="#f00"
? ? ? ? android:centerColor="#0f0"
? ? ? ? android:endColor="#00f"
? ? ? ? android:type="linear"
? ? ? ? android:useLevel="true"/>
</shape>
?
運行結果圖:
????????
?
==================================================================?
? ?那么我們如何做呢?
第一步在這里創建一個文件
?名字叫做btn_normal.xml
?這些東西不要
?把人家寫的東西放里面
?我們做了一個btn_normal.xml? ? ? ? ? ? ? ?未按下情況下 ,正經情況下
再做一個? ? ? ? btn_pressde.xml? ? ? ? 按下的情況下
?里面的代碼
? 還要來個選擇,你最后通過選擇器,選擇用正常的時候的按鍵,還是被按下時候的按鍵
?代碼
正常狀態用????????btn_normal
按下狀態用????????btn_pressed
?用的話很容易用,直接在我們的布局文件? ? ? ? 第60行
?運行一下
?
?按下狀態
?
?修改一下代碼,圓角的半徑改成30
?運行一下
?按下狀態
?包括顏色,正常情況下,我們顯示一個綠色
?運行一下
?按一下
?
總結
以上是生活随笔為你收集整理的Android:相对布局综合小演练—智能家居,按键快速美化的小技巧的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android:Margin和Paddi
- 下一篇: 放u盘里的文件打不开怎么办 U盘文件无法