如何RelativeLayout Align Center android

Bar*_*gul 6 android textview relativelayout

<?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"
android:orientation="vertical" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="78dp"
        android:layout_marginTop="199dp"
        android:text="test" />

     <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:text="I want to align Center" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是我的xml代码.

我想在TextView2中使用layout_alignCenter ="@ + id/textView1".

但是没有alignCenter.

我认为layout_centerVertical或centerinParent也没用.

Ani*_*hav 12

您希望显示文本视图的确切屏幕中心....然后使用下面修改过的代码.

<?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"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_centerInParent="true"
    android:text="test" />

 <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:text="I want to align Center" 
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_centerInParent="true"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这绝对适合你.如果还有其他要求,请详细说明.


aes*_*eis 2

您可以将 a 包装RelativeLayout在 a 中LinearLayout并使用Space元素:

<LinearLayout layout_width="0dp" layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
    <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

    <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="78dp"
            android:layout_marginTop="199dp"
            android:text="test" />

         <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:text="I want to align Center" />

    </RelativeLayout>
    <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

该元素将根据布局权重在Space所有元素之间相对分配空白空间。Space