RelativeLayout中两个控件怎么居中显示

Jack---Jiao 2014-07-01 01:45:35
不居中还有其他控件
现在只想两个控件一起的效果是居中的
怎么设置???
...全文
10279 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jack---Jiao 2014-07-01
  • 打赏
  • 举报
回复
引用 3 楼 i525813382 的回复:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <View android:id="@+id/line" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_centerInParent="true" /> <Button android:id="@+id/show" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_above="@id/line" android:layout_centerHorizontal="true" android:gravity="center" android:paddingBottom="15dp" android:text="演示" /> <Button android:id="@+id/about" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_below="@id/line" android:layout_centerHorizontal="true" android:gravity="center" android:paddingTop="5dp" android:text="介绍" /> </RelativeLayout> 先设置一个View,让其居中,再设置另外两个控件,一个在它上面,一个在它下面。View是一个高为1dp的控件。 第二种方法是用一个LinearLayout将两个控件包起来,将LinearLayout设置为居中。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical" > <Button android:id="@+id/show" android:layout_width="120dp" android:layout_height="wrap_content" android:text="演示" /> <Button android:id="@+id/about" android:layout_width="120dp" android:layout_height="wrap_content" android:text="介绍" /> </LinearLayout> </RelativeLayout>
第二种好使 第一种居中的前提是两个控件的长度相同
i525813382 2014-07-01
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <View android:id="@+id/line" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_centerInParent="true" /> <Button android:id="@+id/show" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_above="@id/line" android:layout_centerHorizontal="true" android:gravity="center" android:paddingBottom="15dp" android:text="演示" /> <Button android:id="@+id/about" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_below="@id/line" android:layout_centerHorizontal="true" android:gravity="center" android:paddingTop="5dp" android:text="介绍" /> </RelativeLayout> 先设置一个View,让其居中,再设置另外两个控件,一个在它上面,一个在它下面。View是一个高为1dp的控件。 第二种方法是用一个LinearLayout将两个控件包起来,将LinearLayout设置为居中。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical" > <Button android:id="@+id/show" android:layout_width="120dp" android:layout_height="wrap_content" android:text="演示" /> <Button android:id="@+id/about" android:layout_width="120dp" android:layout_height="wrap_content" android:text="介绍" /> </LinearLayout> </RelativeLayout>
Jack---Jiao 2014-07-01
  • 打赏
  • 举报
回复
引用 1 楼 hjywyj 的回复:
android:layout_centerHorizontal="true" android:layout_centerVertical="true"
这样的话 两个控件会在中间重叠的啊
  • 打赏
  • 举报
回复
android:layout_centerHorizontal="true" android:layout_centerVertical="true"

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧