Android LinearLayout

LinearLayout又称为线性布局,是一种非常常用的布局,这个布局会将它所包含的控件在线性方向上依次排列

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3"/>

</LinearLayout>

android:orientation属性 确认LinearLayout中的控件是怎么排序的,如果是horizontal则是水平排序的,如果是vertical则是垂直排序的

如果LinearLayout的排列方式是horizontal,内部控件就绝对不能将宽度指定为match_parent,因为这样,单独的一个控件就将整个水平方向占满,其他控件就没有位置可以放了.同样的,如果LinearLayout的排列方式是vertical,内部控件就不能将高度指定为match_parent

android:Layout_gravity属性和android:gravity属性的区别
android:gravity是指定文字在控件中的对齐方式
android:Layout_gravity是用于指定控件在布局中的对齐方式
注意:
当LinearLayout的排序方式为horizontal时,只有垂直方向上的对齐方式才会生效,因为水平方向上的长度是不固定的,每添加一个控件,水平方向上的长度都会改变,因此无法指定该方向上的对齐方式
当LinearLayout的排序方式为vertical时,只有水平上的对齐方式才会生效

android:Layout_weight 属性,这个属性允许我们使用比例的方式来指定控件的大小,在手机屏幕适配性方面可以起到非常重要的作用.其原理非常简单,系统会先把LinearLayout下所有控件指定的Layout_weight值相加,得到一个总值,然后每个控件所占的比例就是用该控件的Layout_weight值除以刚才算出来的总值.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值