簡體   English   中英

相對布局的奇怪邊距問題

[英]Weird margin issue with relative layout

我試圖有一個TextView來填充左側的按鈕和右側的布局邊緣之間的空間。

<TextView
    android:id="@+id/welcomeHeader"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/setInfusionReminder"
    android:layout_marginRight="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:gravity="center"
    android:text="Welcome!" />

我在編輯器中看到的是android:layout_marginRight應用於左側。 最終結果是左邊為40dp裕度,右邊為0裕度。

在此處輸入圖片說明

以下屬性不能像您期望的那樣協同工作:

android:layout_toRightOf="@id/setInfusionReminder"
android:layout_alignParentRight="true"

旁注:與我在自己的項目中所看到的完全一樣,它傾向於使該元素對於與布局相關的其他屬性有些不可控制。

更新以解決評論:

我可能會執行以下操作:

1. Remove the android:layout_alignParentRight="true"
2. Set the android:layout_width to "fill_parent"
3. Keep the gravity at android:gravity => "center"

這應該使它盡可能向右擴展,同時使內容(文本)居中。

我認為您同時使用了android:layout_alignParentRight="true"android:layout_toRightOf="@+id/setInfusionReminder" 刪除android:layout_alignParentRight並嘗試按照您的android:layout_alignParentRight工作。

     <TextView
                android:id="@+id/welcomeHeader"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:layout_alignParentRight="true"
                android:layout_toRightOf="@+id/btnSettkoff"
                android:layout_marginRight="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:text="Welcome!" />

希望能幫助到你。

暫無
暫無

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

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