android基礎之LinearLayout佈局

LinearLayout佈局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:orientation="horizontal"

     >

<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="button1"  

    />

<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="button2"

    />

<Button

    android:layout_height="wrap_content"

    android:layout_width="wrap_content"

    android:text="Button3"

    />

</LinearLayout>

wKiom1Ok-byThkYcAACBGrLC1Nc445.jpg

LinearLayout佈局的嵌套:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:orientation="horizontal"

     >

<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="button1"  

    />

<LinearLayout android:orientation="vertical"

 android:layout_width="fill_parent"

 android:layout_height="wrap_content"

 >

 <Button

 android:text="Button2"   

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

/>

<Button

    android:layout_height="wrap_content"

    android:layout_width="wrap_content"

    android:text="Button3"

    />

</LinearLayout>

</LinearLayout>

wKioL1Ok-czxitJZAACC5WlSnVI216.jpg

Layout_weight屬性:

所有的組件都有這個屬性,不設置的情況下默認爲0,其屬性表示當前有多大視圖就佔據多大視圖

如果其值高於0,則表示將父視圖中的可用空間進行分割

示例代碼如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:orientation="vertical"

     >

<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="button1" 

    android:layout_weight="1" 

    />

<Button

    android:text="button4"

    android:layout_height="wrap_content"

    android:layout_width="wrap_content"

    android:layout_weight="1"

    />

</LinearLayout>

wKiom1Ok-gzxiEBZAAB3tzxyTW0977.jpg

線性佈局中常用的屬性:

1:gravity:每個組件默認其值爲左上角對齊。這個屬性可以調整組件對齊方式比如如何向左。向右或者居中對齊等、

2:padding邊距的填充,也稱內邊距.指的是當前佈局與包含的組件之間的邊距

android:paddingTop

android:paddingBottom

android:paddingLeft

android:paddingRight

android:padding表示周圍四方向個邊距統一調整

3:layout_margin:外邊距。指的是與其他組件之間的邊距。

android:layout_marginTop

android:layout_marginBottom

android:layout_marginLeft

android:layout_marginRight

android:layout_margin表示設置四方向邊距統一調整

一個比較綜合的例子:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:orientation="vertical"

     >

<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="button1" 

    android:layout_gravity="bottom" 

    android:paddingTop="10dp"

    />

<Button

    android:text="button4"

    android:layout_height="wrap_content"

    android:layout_width="wrap_content"

    android:layout_gravity="bottom"

    android:layout_marginLeft="50dp"

    android:layout_marginTop="30dp"

    />

<Button

    android:text="button2"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_gravity="bottom"  

    android:paddingLeft="60dp"

    android:paddingBottom="60dp"

   />

<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="button3"

    android:layout_gravity="right"

    />

<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="button6"

    android:layout_gravity="right"

    android:layout_marginRight="40dp"

    android:layout_marginTop="40dp"

    />

</LinearLayout>

wKioL1Ok-fGAry2-AAEdrcH5frE695.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章