Android相对布局(RelativeLayout)常用属性、练习使用按键、文本框等控件、线性布局(LinearLayout)属性
RelativeLayout中子控件常用屬性:
子控件默認是從父控件的左上角開始排列的
- 相對于父控件
- 相對于給定的ID控件
- 居中
一個簡單的登錄界面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><RelativeLayoutandroid:layout_height="150dp"android:layout_width="400dp"android:layout_centerInParent="true"android:background="#ff0000"><TextViewandroid:id="@+id/user"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="用戶:"android:textSize="20dp"android:textColor="#ffffff"/><EditTextandroid:id="@+id/userline"android:layout_marginTop="5dp"android:layout_width="300dp"android:layout_height="40dp"android:layout_toRightOf="@id/user"/><TextViewandroid:id="@+id/passwd"android:layout_marginTop="10dp"android:layout_below="@id/user"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密碼:"android:textSize="20dp"android:textColor="#ffffff"/><EditTextandroid:id="@+id/passwdline"android:layout_width="300dp"android:layout_height="40dp"android:layout_below="@id/userline"android:layout_toRightOf="@id/passwd"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="確定"android:layout_alignParentRight="true"android:layout_below="@id/passwdline"/></RelativeLayout> </RelativeLayout>所的結果:
布局間的所屬關系:
Margin和Padding:
盒模型(控件)主要定義四個區域:內容 (content)、邊框距(padding)即內邊距、邊界(border)和外邊距(margin)。 對于初學者,經常會搞不清楚margin,background-color,background- image,padding,content,border之間的層次、關系和相互影響。這里提供一張盒模型的平面示意圖,希望便于你的理解和記憶。Margin 是整體移動,帶著控件里面的內容(content),而padding 是移動控件里面的內容相對于控件Bprder的距離。
將上述界面進行美化:
如下圖所示:
安卓按鍵快速美化:
- 在res/drawable目錄下新建按鈕樣式文件 btn_normal.xml(正常狀態) 和 btn_pressed.xml(按下狀態)。
btn_normal.xml文件:
<?xml version="1.0" encoding="utf-8"?> <shapexmlns: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>- 在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>練習制作刷卡界面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_shopping_menu"tools:context=".MainActivity" ><ImageView android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_centerInParent="true"android:src="@drawable/pic_rf"/><ImageViewandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_centerInParent="true"android:src="@drawable/card"android:paddingLeft="100dp"android:paddingTop="50dp"/><Buttonandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_centerHorizontal="true"android:layout_marginBottom="30dp"android:layout_alignParentBottom="true"android:text="刷卡"android:background="@drawable/btn_selector"android:textColor="#ff0000"/><RelativeLayoutandroid:layout_height="wrap_content"android:layout_width="match_parent"android:background="#00ff00"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="30dp"android:text="刷卡界面"android:layout_marginLeft="20dp"/><Buttonandroid:id="@+id/butn1"android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="注冊"android:layout_alignParentRight="true" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="查詢信息"android:layout_toLeftOf="@id/butn1"android:layout_marginRight="20dp"/></RelativeLayout></RelativeLayout>結果如下:
LinearLayout常用屬性(它的父控件還是RelativeLayout,所以RelativeLayout的屬性還可以拿來用):
- orientation: 布局中控件的排列方式,有vertical(豎直,默認)和horizontal(水平)兩種方式
- gravity:控制組件所包含的子元素的對齊方式,可多個組合,如left|buttom(這個是基礎控件相對于父控件來說的)
- layout_gravity: 控制該組件在父容器中的對齊方式,(這個是布局控件相對于父控件來說的)
- layout_width:布局寬度,通常不直接寫數字的,用wrap_content(組件實際大小)和fill_parent或者match_marent(填滿父容器)
- layout_height:布局的高度,參數同上
- background:為組件設置一個背景圖片,或者直接用顏色覆蓋
Weight(權重):
該屬性用來等比例地劃分區域。
- 最簡單的用法:要等比例劃分,分誰,誰為0,Weight按比例即可
- 當我們設置寬度為0dp時,同時設置weight屬性為1,意思就是在寬度方向上所占比重為1。如果將height設置為0,同時設置weight屬性為2,意思就是在豎直方向所占比重為2。
divider分割線:
該屬性用于LinearLayout設置分割圖片,通過showDivider來設置分割線的所在位置,有四個可選值:none、middle、begining、end,當然還可以通過:
- divider: 為LinearLayout設置分割線的圖片
- showDivider: 設置分割線所在位置,有四個通道:none、middle、begining、end
- dividerPadding: 設置分割線的Padding
設置分割線(divider):
然后編輯該分割線的代碼:
使用線性布局和相對布局寫一個丑陋的登錄界面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_shopping_menu"tools:context=".MainActivity" ><LinearLayoutandroid:layout_width="300dp"android:layout_height="wrap_content"android:layout_centerInParent="true"android:orientation="horizontal" ><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:divider="@drawable/divider"android:showDividers="middle|end"android:dividerPadding="2dp"android:orientation="vertical" ><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center"android:textStyle="bold"android:textSize="15dp"android:text="賬號:" /><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center"android:textStyle="bold"android:textSize="15dp"android:text="密碼:" /><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center"android:textStyle="bold"android:textSize="15dp"android:text="ID:" /></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="5"android:orientation="vertical" ><EditTextandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><EditTextandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><EditTextandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /></LinearLayout></LinearLayout></RelativeLayout>結果如下圖所示:
總結
以上是生活随笔為你收集整理的Android相对布局(RelativeLayout)常用属性、练习使用按键、文本框等控件、线性布局(LinearLayout)属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机操作系统(4):操作系统的重要功能
- 下一篇: JAVA--scjp证书。