簡體   English   中英

如何RelativeLayout Align Center android

[英]How to RelativeLayout Align Center android

<?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也沒用。

您希望顯示文本視圖的確切屏幕中心....然后使用下面修改過的代碼。

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

這絕對適合你。 如果還有其他要求,請詳細說明。

您可以在LinearLayout包裝RelativeLayout並使用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元素之間相對分配空白空間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM