標籤: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>
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/30/55/wKiom1Ok-byThkYcAACBGrLC1Nc445.jpg" title="2014-06-21_100921.png" alt="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>
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/30/55/wKioL1Ok-czxitJZAACC5WlSnVI216.jpg" title="2014-06-21_101410.png" alt="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>
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/30/55/wKiom1Ok-gzxiEBZAAB3tzxyTW0977.jpg" title="2014-06-21_104118.png" alt="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>
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/30/55/wKioL1Ok-fGAry2-AAEdrcH5frE695.jpg" title="2014-06-21_111310.png" alt="wKioL1Ok-fGAry2-AAEdrcH5frE695.jpg" />
本文出自 “hagar” 部落格,請務必保留此出處http://7832308.blog.51cto.com/7822308/1429181