之前都是使用相对布局进行居中控件的,今天看了一篇博客,突然明白,原来使用weight 可以让linearlayout内部的控件居中显示,实在腻害,下面就说下最近掌握的weight的使用方法
- 首先说下linearlayout使用weight来让内部的控件居中的方法
如果需要调整总间控件的大小可以通过调整父控件和子控件所占的比例
<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将底部的控件显示出来
使用权重的方法我们就可以很轻松的将占满的屏幕,挤出一部分空间给底部使用了,突然感觉很神奇的样子,遇到这种问题再也不用去换成相对布局了,很开心有木有
<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>