您的位置:首页 > 其它

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

2017-08-19 16:34 615 查看
<?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>

第二种好使 第一种居中的前提是两个控件的长度相同 

Url连接:http://bbs.csdn.net/wap/topics/390824491
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: