Android中设置半个屏幕大小且居中的按钮布局 (layout_weight属性)
16lz
2021-01-24
先看如下布局 :
上图中,按钮的大小为屏幕的一半,然后居中显示在布局中央,每个人心中都有自己的答案,看看我的方法吧,布局布局xml如下 :
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:gravity="center" android:orientation="horizontal" android:weightSum="1" > <!-- 1.将LinearLayout的layout_weight设置为1 --> <!-- 2.将Button的layout_width设为0dp, 并且将layout_weight设置为0.5 --> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.5" android:textSize="13sp" android:text="@string/button_text" > </Button></LinearLayout>这里有几个关键点:
- 布局为LinearLayout,以便可以设置layout_weight;
- 设置LinearLayout的android:weightSum的值为1;
- 将Button的layout_width设置为0dp;
- 将Button的layout_weight的值设置为0.5, 即上面的android:weightSum的一半。
更多相关文章
- android的动画相关参数说明
- Android评论功能的实现
- ReactNative之Android绝对布局position:'absolute'问题
- Android(安卓)Reveal圆形Activity转场动画
- Android开发艺术探索笔记 第四章 View的工作原理
- Android四大控件之Activity详解:实践篇
- Android中Fragment的使用
- Android(安卓)中自定义View的应用.
- Android圆形进度条控件-CircleSeekBar