簡體   English   中英

Android layout_margin與RelativeLayout屬性沖突

[英]Android layout_margin conflicts with RelativeLayout attributes

首先,一張圖片(來自hierarchyviewer):

好的和壞的按鈕

我有兩個按鈕,它們的文本長度不同,我希望其大小相同。 在圖像中,您可以看到此功能在頂部。 但是,我希望按鈕之間有一些空間,因此對於第三個按鈕和第四個按鈕,我添加了layout_margin屬性,為它們提供了一些空間。 混亂隨之而來。 第四個按鈕保持相同大小,並添加適當的邊距。 但是第三個按鈕似乎沒有在右邊添加邊距,因此會失去寬度。

我認為正在發生的事情是在對齊之后應用了邊距,因此...實際上,我無法提出一個可以完全解釋它的流程。

我想要的是:

  • 要了解這些東西是如何工作的-視圖的布局對我來說似乎是不直觀的,而且我無法想象達到我不會隨機更改屬性來解決這種問題的地步。 是否有詳盡的教程可以解決問題?

  • 兩個不同長度的等長按鈕以相對的邊距在相對布局中顯示文本。 我需要相對布局來安排該活動的所有其他視圖。

代碼如下。

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    >
    <Button
        android:id="@+id/goodThis"
        android:text="Short text"
        android:background="@drawable/button_red" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/goodThat"
    />
    <Button
        android:id="@+id/goodThat"
        android:text="Longer text"
        android:background="@drawable/button_green" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"
        android:layout_below="@+id/goodThis"
    />

    <Button
        android:id="@+id/badThis"
        android:text="Short text"
        android:background="@drawable/button_red" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_below="@+id/goodThat"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/badThat"
        android:layout_margin="3dp"
    />
    <Button
        android:id="@+id/badThat"
        android:text="Longer text"
        android:background="@drawable/button_green" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"
        android:layout_below="@+id/badThis"
        android:layout_margin="3dp"
    />
</RelativeLayout>

為了使我的回答比上面的評論更能作為答案,我從RelativeLayout的經驗中學到了以下內容:

1)消除前向ID引用。 這會使確定該方法變得更加困難,但是您可以看到,解析此類引用將需要在運行時至少兩次通過布局規范,並可能使事情復雜化。

2)我通常使用特定的邊距/填充屬性,例如layout_marginTop等,以獲得更好的結果。 您使用橡皮布“ layout_margin”會使第3個和第4個按鈕稍微向右偏移,這可能不是您想要的外觀。 嘗試僅使用marginTop來隔開按鈕。

暫無
暫無

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

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