1.LinearLayout (線性布局)
?它是一個視圖組,所有的子元素都是單個方向的:
vertically(垂直) or horizontally(水平)套啤,要么都是垂直帝牡,要么都是水平婴洼。
屬性(attribute):android:orientation 海蔽。
屬性:android:layout_weight="1".
Google官方推薦,當使用weight屬性時输硝,將width設為0dip即可今瀑,效果跟設成wrap_content是一樣的。這樣weight就可以理解為占比了点把!
layout_width="wrap_content"只是在子LinearLayout布局下使用橘荠,并且有的也建議改成0dp.
(1)新建項目-androidLinearLayout
(2)修改layout的布局文件activity_main.xml:
? ?默認下的布局是RelativeLayout,(相對布局),
? ?然后就改成LinearLayout.
(3)編寫一個實例-用戶登錄界面(采用LinearLayout布局)
效果圖:
設計界面(可視化界面):
AVD上的效果圖:
?思路:從大到小的布局郎逃,均采用LinearLayout線性布局哥童。
? ? ? ? ? ? ? 再添加控件,進行里面的細節(jié)修改褒翰。
layout的布局文件activity_main.xml:
<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" #大布局-垂直
? ? tools:context="com.example.andriod_linearlayout.MainActivity" >
? ? <LinearLayout
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"? #根據內容設置
? ? android:background="#CCCCCC" #背景顏色
? ? android:orientation="horizontal">? #小布局-水平
? ? ? ? <TextView
? ? ? ? ? ? android:id="@+id/textView1"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="@string/username" /> #在values 的strings.xml下定義贮懈,消除下警告(黃色波浪線)
? ? ? ? <EditText
? ? ? ? ? ? android:id="@+id/editText1"
? ? ? ? ? ? android:layout_width="0dp"(消除黃色波浪線)
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_weight="1"(“權重”)
? ? ? ? ? ? android:ems="10" >
? ? ? ? ? ? <requestFocus />
? ? ? ? </EditText>
</LinearLayout>
? ? <LinearLayout
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:background="#CCCCCC"
? ? android:orientation="horizontal">
? ? ? ? <TextView
? ? ? ? ? ? android:id="@+id/textView2"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:text="@string/password" />
? ? ? ? <EditText
? ? ? ? ? ? android:id="@+id/editText2"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_weight="0.76"
? ? ? ? ? ? android:ems="10"
? ? ? ? ? ? android:inputType="textPassword" />
? ? ? ? </LinearLayout>
? <LinearLayout
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:background="#CCCCCC"
? ? android:gravity="center"
? ? android:orientation="horizontal">
? ? ? <Button
? ? ? ? ? android:id="@+id/button1"
? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? android:layout_weight="1"#權重?
? ? ? ? ? android:text="@string/login" />
? ? ? <Button
? ? ? ? ? android:id="@+id/button2"
? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? android:layout_weight="1"
? ? ? ? ? android:text="@string/reset" />
? ? ? </LinearLayout>
</LinearLayout>
strings.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
? ? <string name="app_name">andriod_linearlayout</string>
? ? <string name="hello_world">Hello world!</string>
? ? <string name="action_settings">Settings</string>
? ? <string name="username">用戶名:</string>
? ? <string name="password">密碼:</string>
? ? <string name="login">登錄</string>
? ? <string name="reset">取消</string>
</resources>