2011-08-30 144 views
6

我有兩個按鈕,我想水平並排顯示,但它們一起填滿了手機的水平長度。高度是包裝內容,沒關係。我現在的問題是隻有一個按鈕顯示出來(橫跨屏幕)。Android按鈕佈局 - 在整個屏幕上並排獲取兩個按鈕

這裏是我的XML代碼:

<LinearLayout 
    android:id="@+id/page_buttons" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 

    > 
    <Button 
     android:id="@+id/prevButton" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Previous" 
    /> 

    <Button 
     android:id="@+id/nextButton" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Next" 
    /> 

+0

你究竟如何期待兩個按鈕出現在屏幕上,因爲你已經在xml中指定了一個按鈕?你是否動態創建第二個並將其添加到線性佈局? – asenovm

+0

我編輯了這篇文章,使其現在可讀。 – JDS

回答

23

更改按鈕的XML包括layout_weight屬性:

<Button android:id="@+id/nextButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="Next"/> 
+0

除了接受的答案之外,我認爲值得指出的是,包裝這兩個按鈕的線性佈局應該有'android:orientation =「horizo​​ntal」'選項。 我很慢,一開始沒有理解。 – w3bshark

5
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<Button 
android:id="@+id/button01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_below="@id/entry" 
android:layout_alignParentLeft="true" 
android:layout_toLeftOf="@+id/space" 
/> 
<TextView 
android:id="@id/space" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true"/> 
<Button 
android:id="@+id/button02" 
android:layout_toRightOf="@id/button01" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_below="@id/entry" 
android:layout_alignParentRight="true" 
/> 
</RelativeLayout> 

好像你想兩個相等的按鈕,不包裝的內容。我使用TextView創建了一個居中的間隔,並且與之相對。左按鈕到左側父母和間隔,右側按鈕到左側按鈕和父母右側。

0

您的要求是

<LinearLayout 
    android:id="@+id/page_buttons" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 

    > 
    <Button 
     android:id="@+id/prevButton" 
     android:layout_width="0dp" 
     android:weight="0.5" 
     android:layout_height="wrap_content" 
     android:text="Previous" 
    /> 

    <Button 
     android:id="@+id/nextButton" 
     android:layout_width="0dp" 
     android:weight="0.5" 
     android:layout_height="wrap_content" 
     android:text="Next" 
    /> 
0
<?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="wrap_content" 
    android:layout_centerHorizontal="true"> 

    <TextView 
     android:text="@+id/SomeText" 
     android:id="@+id/TextView01" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:background="@android:drawable/bottom_bar" 
     android:paddingLeft="4.0dip" 
     android:paddingTop="5.0dip" 
     android:paddingRight="4.0dip" 
     android:paddingBottom="1.0dip" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:layout_below="@+id/TextView01"> 

     <Button 
      android:id="@+id/allow" 
      android:layout_width="0.0dip" android:layout_height="fill_parent" 
      android:text="Allow" 
      android:layout_weight="1.0" /> 

     <Button 
      android:id="@+id/deny" 
      android:layout_width="0.0dip" android:layout_height="fill_parent" 
      android:text="Deny" 
      android:layout_weight="1.0" /> 

    </LinearLayout> 
</RelativeLayout> 
+2

你應該提供一些解釋和代碼。 –

13

由於沒有人解釋爲什麼他們的解決方案的工作,我給它一展身手。

問題在於按鈕的佈局。這兩個相關的屬性是layout_width和layout_weight。

在其他佈局系統中,當您指示佈局的每個元素都必須填充父元素(layout_width =「fill_parent」)時,他們通過平均分配父元素之間的空間來實現此目的。所以,他們每個人都會有相同的大小。

相反,Android的佈局系統,如果這兩個元件具有 layout_width =「FILL_PARENT」的第一個元素(在你的情況下, 預覽按鈕)將被拉伸以填充父和第二 (或第三或其他等)將不會留下任何空間來分發,因此它不會顯示。 將不可見。

現在,爲了使它理解您希望兩個按鈕都顯示,您可以爲每個按鈕設置layout_weight。要使這些按鈕具有相同的尺寸,請爲它們設置相同的佈局權重。

layout_weight定義每個按鈕佔用的父級的「部分」(或片段)數量。父母會被分成若干段,等於子段的總和。如果您想讓一個按鈕比另一個大三倍,則必須爲其分配與第一個按鈕的部件數相等的部件數乘以三。對於下一步按鈕layout_weight = 1

  • 所以,如果你希望你的下一個按鈕是那麼 預覽按鈕大兩倍,你可以這樣做:

    • 的預覽按鈕layout_weight = 2

    其結果是,親將在3個部分,其中2個將被分配給Next按鈕和1到上一個被切割瀏覽按鈕。

    這裏採用的示例是按鈕和水平佈局,但這對任何類型的對象以及垂直佈局父項都適用。

  • +0

    非常感謝您的解釋 – Joqus

    1

    你的按鈕的父級是線性佈局,並且你將Button的寬度設置爲填充父級,因此它佔據了整個空間。你有兩種選擇來實現這個...

    1. 給你的按鈕固定寬度(例如50dip)。
    2. 給寬度爲0dp和插入在這兩個按鈕「重量」多一個屬性,並給每個0.5dip ...
    0

    我發現了仍然似乎沒有得到他們想要的人的主要問題。 當使用常規(開箱即用/默認)背景的按鈕時,它具有內置保證金。 如果您嘗試用單一顏色手動更改背景,可以輕鬆地告訴您只需設置單獨的背景。 除此之外,當你有權重投入時,它會工作。