Android布局二_LinearLayout

來源:互聯網
上載者:User

         LinearLayou(線性布局布局)

一些重要的屬性:


           一 orientation(朝向)   該屬性值有兩種一種是垂直朝向(verticle),還有一個是水平朝向(horizontal)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:background="@drawable/blue"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <!-- view1 goes on top -->    <TextView        android:background="@drawable/box"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/linear_layout_1_top"/>    <!-- view2 goes in the middle -->    <TextView        android:background="@drawable/box"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/linear_layout_1_middle"/>    <!-- view3 goes on the bottom -->    <TextView        android:background="@drawable/box"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/linear_layout_1_bottom"/></LinearLayout>

結果如下:


                     二 layout_weight(權重): 

看下面一個例子: 該布局填充整個螢幕,其中有三個字控制項,分別佔據頭部,底部,中間

在上一篇部落格中我們通過相對布局也可以實現(把高設定成0,height=0)

更多關於該屬性的細節可以瀏覽http://hi.baidu.com/mendynew/item/39cd374192770bab60d7b915

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:background="@drawable/blue"    android:layout_width="match_parent"    android:layout_height="match_parent">    <!-- view1 goes on top -->    <TextView        android:background="@drawable/box"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/linear_layout_3_top"/>    <!-- view2 goes in the middle -->    <TextView        android:background="@drawable/box"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="@string/linear_layout_3_middle"/>    <!-- view3 goes on the bottom -->    <TextView        android:background="@drawable/box"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/linear_layout_3_bottom"/></LinearLayout>

運行結果:

下面一個例子,所有子空間的都是相同的寬度.也是通過該屬性來實現的.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:background="@drawable/red"        android:layout_width="0dip"        android:layout_height="match_parent"        android:layout_weight="1"/>    <TextView        android:background="@drawable/green"        android:layout_width="0dip"        android:layout_height="match_parent"        android:layout_weight="1"/>    <TextView        android:background="@drawable/blue"        android:layout_width="0dip"        android:layout_height="match_parent"        android:layout_weight="1"/>    <TextView        android:background="@drawable/yellow"        android:layout_width="0dip"        android:layout_height="match_parent"        android:layout_weight="1"/></LinearLayout>

下面看一個簡單表單的例子,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:background="@drawable/blue"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:padding="10dip">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/linear_layout_5_instructions"/>    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@android:drawable/editbox_background"/>    <LinearLayout        android:orientation="horizontal"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="right" >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/linear_layout_5_cancel"/>        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="10dip"            android:text="@string/linear_layout_5_ok" />    </LinearLayout></LinearLayout>

在上一篇部落格中通過相對布局也能布局出這樣的,但是從效率上說,相對布局要好很多,效率要高.從這個例子上看線性布局的層級要深.


weight屬性還可以實現如下布局:


運行結果:


通過相對布局也是可以實現這樣的布局,把button設定為android:layout_alignParentBottom ="true"


                       三 gravity(重心)

下面來看一個例子:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:padding="30dip" >    <LinearLayout        android:id="@+id/layout"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@drawable/blue"        android:orientation="vertical"        android:padding="30dip" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@drawable/box"            android:text="@string/linear_layout_8_c" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@drawable/box"            android:text="@string/linear_layout_8_b" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@drawable/box"            android:text="@string/linear_layout_8_c" />    </LinearLayout></FrameLayout>


Java代碼:

@Override    public boolean onCreateOptionsMenu(Menu menu) {        super.onCreateOptionsMenu(menu);        menu.add(0, VERTICAL_ID, 0, R.string.linear_layout_8_vertical);        menu.add(0, HORIZONTAL_ID, 0, R.string.linear_layout_8_horizontal);        menu.add(0, TOP_ID, 0, R.string.linear_layout_8_top);        menu.add(0, MIDDLE_ID, 0, R.string.linear_layout_8_middle);        menu.add(0, BOTTOM_ID, 0, R.string.linear_layout_8_bottom);        menu.add(0, LEFT_ID, 0, R.string.linear_layout_8_left);        menu.add(0, CENTER_ID, 0, R.string.linear_layout_8_center);        menu.add(0, RIGHT_ID, 0, R.string.linear_layout_8_right);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {        case VERTICAL_ID:            mLinearLayout.setOrientation(LinearLayout.VERTICAL);            return true;        case HORIZONTAL_ID:            mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);            return true;        case TOP_ID:            mLinearLayout.setVerticalGravity(Gravity.TOP);            return true;        case MIDDLE_ID:            mLinearLayout.setVerticalGravity(Gravity.CENTER_VERTICAL);            return true;        case BOTTOM_ID:            mLinearLayout.setVerticalGravity(Gravity.BOTTOM);            return true;        case LEFT_ID:            mLinearLayout.setHorizontalGravity(Gravity.LEFT);            return true;        case CENTER_ID:            mLinearLayout.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);            return true;        case RIGHT_ID:            mLinearLayout.setHorizontalGravity(Gravity.RIGHT);            return true;        }        return super.onOptionsItemSelected(item);    }

以上設定的gravity是通過Java代碼設定的,也可以通過xml配置

需要注意的是layout_gravity和gravity的區別,前者是該控制項相對於父控制項的重心(gravity),後者該控制項的子空間的重心(gravity)




歡迎轉載,轉載請註明出處: http://blog.csdn.net/johnny901114/article/details/7866864






相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.