0

LinearLayout我們可以通過使用Layout_Weight使它們佔據所有寬度並且每個寬度取決於它們的Layout_Weight值(或比率)來水平填充元素。我的問題是如何在RelativeLayout的情況下做同樣的事情。沒有像Layout_Weight這樣的屬性。 我在我的項目中使用了RelativeLayout,它包含4個按鈕,它們需要位於屏幕的底部,並且必須填滿整個寬度。如果我使用硬編碼Layout_Width="20dp"或類似的東西。當我將縱向從縱向更改爲橫向時,它會產生問題,反之亦然。什麼與layout_weight相似

<?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" 
    android:orientation="vertical" > 

     <Button 
      android:id="@+id/feed_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:text="@string/feed" 
      android:textColor="#FF000000" /> 

     <Button 
      android:id="@+id/iwant_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_toRightOf="@id/feed_button" 
      android:text="@string/iwant" 
      android:textColor="#FF000000" /> 

     <Button 
      android:id="@+id/share_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_toRightOf="@id/iwant_button" 
      android:text="@string/share" 
      android:textColor="#FF000000" /> 

     <Button 
      android:id="@+id/profile_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_toRightOf="@id/share_button" 
      android:text="@string/profile" 
      android:textColor="#FF000000" /> 


</RelativeLayout> 

回答

0

在相對佈局中,我們可以動態實現此目的。

DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics); 

int height = metrics.heightPixels; 
int width = metrics.widthPixels; 

Button1.setWidth(width/2); 
Button2.setWidth(width/2); 
0

作出相對佈局充分寬度

android:layout_width="fill_parent" 

它在底部

android:layout_alignParentBottom="true" 
+0

如果我使用填補母公司的每個按鈕只有最後一個按鈕將是可見....其餘全部將得到重疊 – Housefly 2012-04-04 10:08:51

+0

OK,然後讓他們WRAP_CONTENT代替FILL_PARENT – vipin 2012-04-04 10:16:26

+0

看到沒有什麼相對佈局爲重你可以使他們wrap_content或您在運行時創建它們計算每個按鈕的寬度作爲父寬度/ 4然後分配給按鈕仍然存在,你必須搞砸按鈕的位置 – vipin 2012-04-04 10:21:57

0

我認爲有像Relative layoutlayout_weight沒有屬性添加。 但是android已經給出了Linear Layout的功能。在那種情況下,你爲什麼要用Relative layout

我想你知道如何使用layout_weightLinear Layout

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" > 

    <Button 
     android:id="@+id/feed_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.5" 
     android:text="feed" 
     android:textColor="#FF000000" /> 

    <Button 
     android:id="@+id/iwant_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.5" 
     android:text="iwant" 
     android:textColor="#FF000000" /> 


</LinearLayout> 

我知道你有強制使用相對佈局,但如果你把上面的代碼在你的活動就顯示在顯示器的底部所有按鈕和如果想在低於任何其他佈局,以顯示這些按鈕的佈局,然後使用android:layout_below attributte<LinearLayout >標籤和使用最外面的佈局相對佈局

感謝

+0

是啊...但問題是我的項目需要相對佈局的原因很多....由於這個原因,我不能切換到線性佈局 – Housefly 2012-04-04 10:10:54

+0

是啊.......... ....... – Housefly 2012-04-04 10:13:18

+0

嗨:你在使用某個android項目@ ps嗎? – Housefly 2012-04-04 10:17:17

0

,以保證鈕釦都是平等的與將它們放在一個LinearLayout填充你想要的按鈕涵蓋像這樣面積的唯一方法:

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

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:orientation="vertical" > 
     <Button 
      android:id="@+id/feed_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="@string/feed" 
      android:textColor="#FF000000" /> 

     <Button 
      android:id="@+id/iwant_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:layout_toRightOf="@id/feed_button" 
      android:text="@string/iwant" 
      android:textColor="#FF000000" /> 

     <Button 
      android:id="@+id/share_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="@string/share" 
      android:textColor="#FF000000" /> 

     <Button 
      android:id="@+id/profile_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="@string/profile" 
      android:textColor="#FF000000" /> 
    </LinearLayout> 

</RelativeLayout> 

LinearLayout將填充父佈局的整個寬度,並根據它們的重量均勻分配按鈕。

如果你有兩種佈局,你想在RelativeLayout中均勻分配,那麼這裏有一個非常好的技巧。創建一個0x0像素的「不可見」佈局,將其與父視圖的中心對齊,然後將兩個佈局與相對於您想要的位置的不可見佈局對齊。

相關問題