2012-09-18 145 views
3

下面是我的XML代碼:如何在RelativeLayout中設置相同的高度?

<RelativeLayout 
    android:id="@+id/ib" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:gravity="center" > 

    <ImageButton 
     android:id="@+id/button_icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:id="@+id/tv" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toLeftOf="@+id/ib" 
     android:gravity="center" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 
</RelativeLayout> 

的ImageButton的IB和TextView的總重量有不同的高度。
我希望ib和tv擁有相同的高度。
也就是說,如果ib> tv的高度,則將電視機的高度設置爲與ib相同。
如果不是,則將ib的高度設置爲與tv相同。
我該怎麼做?

回答

3

使用LinearLayout並將ImageButton高度設置爲"wrap_content"並將TextView設置爲"fill_parent"

+0

RelativeLayout中可能嗎? – brian

+0

我不認爲這是可能的使用RelativeLayout。如果使用相對佈局,則必須指定ImageButton的maxHieght,以便它永遠不會將ImageButton拉伸到圖像高度。 – knvarma

1

你需要做高度ImageButton"wrap_content"並有TextView只是"fill_parent"。您需要將它們都包裹在佈局父項中。這樣他們與相同的佈局父項關聯。

+0

但父RelativeLayout的高度WRAP_CONTENT 。它會導致高度全屏? – brian

+0

否... RelativeLayout高度是wrap_content意味着RelativeLayout高度將取決於其中的內容(ImageButton,TextView等..)的高度... –

11

添加android:layout_alignBottom="@+id/button_icon"屬性的TextView

+0

工程像魔術! – deathangel908

0

把你的元素融入到LinearLayout中,設置layout_width = 「match_parent」,並添加屬性 「layout_weight」= 「1」

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="sample text"/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="sample text"/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="sample text"/> 
    </LinearLayout> 
相關問題