【问题标题】:How to RelativeLayout Align Center android如何RelativeLayout对齐中心android
【发布时间】:2014-06-23 23:42:30
【问题描述】:
<?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>

这是我的 xml 代码。

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

但是没有alignCenter。

我认为 layout_centerVertical 或 centerinParent 也没用。

【问题讨论】:

    标签: android textview android-relativelayout


    【解决方案1】:

    您想在屏幕的正中心显示文本视图....然后使用以下修改后的代码。

    <?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>
    

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

    【讨论】:

      【解决方案2】:

      您可以将RelativeLayout 包装在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>
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-22
        • 1970-01-01
        相关资源
        最近更新 更多