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


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"水平,就出现这个效果了




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值