Android组件到父容器距离,android 相对布局 水平平分父控件容器

本文作者分享了一种在相对布局中让两个Button水平平均分布的方法,通过添加中间参考TextView并利用layout属性巧妙布局。布局代码详述了这一过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚才用相对布局的时候,需要用到子控件水平平分父控件的容器。可是在百度上找了半天没有什么可行的办法,所以就自己研究了下。现在我发现了一种比较麻烦的方法,不过能解决问题也算可以了,如果有朋友有更好的方法,希望不吝告诉下。好了,我所说的方法就是,在两个 button 中间 再放一个textview 或者其他什么的,只是用来作为一个中间位置参考的,将textView 设置水平居中(android:layout_centerHorizontal="true"),然后 分别将需要水平平分父容器的两个button 设置 android:layout_toLeftOf="@+id/textview "和android:layout_toRightOf="@+id/textview"属性,最后将两个button的宽度设置fill_parent 就行了。

下面是测试的布局代码:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.first_summary.MainActivity$PlaceholderFragment" >

android:id="@+id/ImageButton"

android:contentDescription="@+id/ImageButton"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

android:id="@+id/up"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/ImageButton"

android:layout_toLeftOf="@+id/cencer"

android:textSize="12sp"

android:text="上传数据"

/>

android:id="@+id/cencer"

android:layout_width="0dip"

android:layout_height="wrap_content"

android:layout_below="@+id/ImageButton"

android:layout_centerHorizontal="true"

/>

android:id="@+id/add"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/ImageButton"

android:layout_toRightOf="@+id/cencer"

android:textSize="12sp"

android:text="添加数据"

/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值