android基础

来源:互联网 发布:兰精莫代尔内裤知乎 编辑:程序博客网 时间:2024/05/22 04:36
 

Layout布局

Layout布局主要有线性布局、表格布局、相对布局、帧式布局。

现形布局: LinearLayout

fill_parent:全部填满

android:layout_width="fill_parent"。代表布局的宽

android:layout_height="fill_parent"代表布局高

android:orientation="vertical(水平)"定位


<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">


<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/name"

<--设置字体-->

android:textSize="20px"/>

<EditText

android:id="@+id/text1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"/>

//嵌套了一个相对布局

<RelativeLayout

android:layout_width="fill_parent"

<--wrap_content包裹内容->

android:layout_height="wrap_content">


<Button

android:id="@+id/btn1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

//layout_alignParentTop :居于容器内顶部

// layout_alignParentRight居于容器内右边

android:layout_alignParentRight="true"

android:layout_alignParentTop="true"

android:text="@string/login_btn"/>


<Button

android:id="@+id/btn2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

// layout_marginRight="25dp@id/btn1元素的距离为25dp

// android:layout_toLeftOf="@id/btn1"在指定@id/btn1"的左边

android:layout_marginRight="25dp"

android:layout_toLeftOf="@id/btn1"

android:text="@string/caile_btn"/>


"

</RelativeLayout>


</LinearLayout>

表格布局:

<?xmlversion="1.0"encoding="utf-8"?>

<TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TableRow>

<TextVie

//android:gravity表示组件的子组件在组件中的位置

//android:padding指定控件的内边距,也就是说控件当中的内容



android:gravity="center"

android:padding="3dip"

android:text="@string/username"

/>

<TextView

android:gravity="center"

android:padding="3dip"

android:text="@string/sex"/>

<TextView

android:gravity="center"

android:padding="3dip"

android:text="@string/age"/>"

</TableRow>

<TableRow>

<TextView

android:layout_gravity="center"

android:padding="3dip"

android:text="@string/usernamez"/>

<TextView

android:layout_gravity="center"

android:padding="3dip"

android:text="@string/sexz"/>

<TextView

android:layout_gravity="center"

android:padding="3dip"

android:text="@string/agez"/>

</TableRow>

<TableRow>

<TextView

android:layout_gravity="center"

android:padding="3dip"

android:text="@string/usernamel"/>

<TextView

android:layout_gravity="center"

android:padding="3dip"

android:text="@string/sexl"/>

<TextView

android:layout_gravity="center"

android:padding="3dip"

android:text="@string/agel"/>

</TableRow>


</TableLayout>

相对布局:该例子成梅花的形状

<?xmlversion="1.0"encoding="utf-8"?>

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

//match_parent=fill_parent

ndroid:layout_width="match_parent"

android:layout_height="match_parent">


<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

//设置背景图片

android:background="@drawable/leaf"

// layout_centerInParent  居中布局

android:layout_centerInParent="true"

android:id="@+id/leaf1"

/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/leaf"

// layout_above@id/leaf1之上

// android:layout_toLeftOf@id/leaf1地左边

android:layout_above="@id/leaf1"

android:layout_toLeftOf="@id/leaf1"

android:id="@+id/leaf2"

/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/leaf"

android:layout_below="@id/leaf1"

android:layout_toLeftOf="@id/leaf1"

android:id="@+id/leaf3"

/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/leaf"

android:layout_above="@id/leaf1"

android:layout_toRightOf="@id/leaf1"

android:id="@+id/leaf4"

/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/leaf"

android:layout_below="@id/leaf1"

android:layout_toRightOf="@id/leaf1"

android:id="@+id/leaf5"

/>

</RelativeLayout>

总结:



  熟练掌握以下重要属性,并灵活运用:


  android:layout_centerInParent


  居中布局


  android:layout_centerVertical


  水平居中布局


  android:layout_centerHorizontal


  垂直居中布局


  android:layout_alignParentTop


  居于容器内顶部


  android:layout_alignParentBottom


  居于容器内底部


  android:layout_alignParentLeft


  居于容器内左边


  android:layout_alignParentRight


  居于容器内右边


  android:layout_above


  居于指定View的上方


  android:layout_below


  居于指定View的下方


  android:layout_toRightOf


  在指定View的右边


  android:layout_toLeftOf


  在指定View的左边


  android:layout_alignTop


  与指定ViewTop一致