您的位置:首页 > 其它

使用weight 让linearlayout内部的控件居中显示

2016-01-11 15:35 169 查看
之前都是使用相对布局进行居中控件的,今天看了一篇博客,突然明白,原来使用weight 可以让linearlayout内部的控件居中显示,实在腻害,下面就说下最近掌握的weight的使用方法

首先说下linearlayout使用weight来让内部的控件居中的方法

如果需要调整总间控件的大小可以通过调整父控件和子控件所占的比例

[code] <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:weightSum="3" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:background="#ff0000"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:text="中间" />
    </LinearLayout>


接下来说下使用底部控件被占满时如何使用weight将底部的控件显示出来

使用权重的方法我们就可以很轻松的将占满的屏幕,挤出一部分空间给底部使用了,突然感觉很神奇的样子,遇到这种问题再也不用去换成相对布局了,很开心有木有

[code]<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" >

  <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:gravity="center"
            android:layout_weight="1"
            android:textSize="24sp"
            android:text="都是我的" />
   <TextView
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:background="#ff0000"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:text="我在底部还是可以显示出来哦" />

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