您的位置:首页 > 移动开发 > Android开发

android基础——界面布局(二) 线性布局

2012-03-16 20:12 423 查看
LinerLayout 是一行(列)只能放置一个控件 的线性布局,所以当有很多控件需要列出来的时候,就可以用这个拉。

代码:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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:gravity="center_vertical"
android:textSize="15pt"
android:background="#aa0000"
android:layout_weight="1"
android:text="@string/hang1"
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="15pt"
android:background="#00aa00"
android:layout_weight="1"
android:text="@string/hang2"
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="15pt"
android:background="#0000aa"
android:layout_weight="1"
android:text="@string/hang3"
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="15pt"
android:background="#aaaa00"
android:layout_weight="1"
android:text="@string/hang4"
/>

</LinearLayout>


string.xml中的代码


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

<resources>

<string name="hello">Hello World, AndroidBookLinerLayoutp101Activity!</string>

<string name="app_name">垂直线性布局</string>

<string name="hang1">第一行</string>

<string name="hang2">第二行</string>

<string name="hang3">第三行</string>

<string name="hang4">第四行</string>

</resources>



解析:

orientation="vertical"表示这个是垂直线性布局

fill_parent 表示该控件填充整个屏幕

wrap_content表示该控件随文字栏位的不同而改变宽度或高度

gravity指明所在位置,上下左右中等等

layout_weight:默认值为0,表示需要多大的视图就占据多大的空间; 大于0,则需要按父类视图的可用空间进行分割,分割大小取决于该值的大小和在当前所占的比例 

android:text="@string/hang1":引用string里面的hang1文本。这个很容易理解的了。就这样规定的

 android:background="#aa0000"背景色








把上面的垂直改为 android:orientation="horizontal"水平,就出现这个效果了

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息