2016-03-28 40 views
0

我想創建一個包含6個NumberPickers和2個按鈕的彈出對話框。我的問題是,過去NumberPicker熄滅對話框的邊緣爲紅色這裏看到(它實際上犯規出現在屏幕上): 下面是XML代碼: Android對話內容不適合

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/np" 
     android:layout_centerHorizontal="true"> 

     <NumberPicker 
      android:id="@+id/numberPicker1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="32dp" 
      android:descendantFocusability="blocksDescendants"/> 

     <NumberPicker 
      android:id="@+id/numberPicker2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="32dp" 
      android:descendantFocusability="blocksDescendants"/> 

     <NumberPicker 
      android:id="@+id/numberPicker3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="32dp" 
      android:descendantFocusability="blocksDescendants"/> 

     <NumberPicker 
      android:id="@+id/numberPicker4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="32dp" 
      android:descendantFocusability="blocksDescendants"/> 

     <NumberPicker 
      android:id="@+id/numberPicker5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="32dp" 
      android:descendantFocusability="blocksDescendants"/> 

     <NumberPicker 
      android:id="@+id/numberPicker6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="32dp" 
      android:descendantFocusability="blocksDescendants"/> 
    </LinearLayout> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Set" 
     android:layout_below="@+id/np" 
     android:layout_alignParentEnd="true" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Cancel" 
     android:layout_below="@+id/np" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 
代碼

,而且我用它簡單地與:

final Dialog dialog = new Dialog(EditTextActivity.this); 
     dialog.setContentView(R.layout.registration_picker_dialog); 
     dialog.setTitle("Edit Year"); 

是否有什麼在XML我可以做,使其收縮的NumberPickers寬度適合?

+0

在每個NumberPicker上,將layout_width設置爲0dp,將layout_weight設置爲1。 –

回答

1

在每個NumberPicker上使用layout_weight屬性給每個控件容器的1/6。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/np" 
    android:layout_centerHorizontal="true" 
    android:weightSum="1.0"> <!-- Added weightSum --> 

    <!-- added layout_weight --> 
    <NumberPicker android:layout_weight="0.167" 
     android:id="@+id/numberPicker1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="32dp" 
     android:descendantFocusability="blocksDescendants"/> 

    <!-- added layout_weight --> 
    <NumberPicker android:layout_weight="0.167" 
     android:id="@+id/numberPicker2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="32dp" 
     android:descendantFocusability="blocksDescendants"/> 

    <!-- added layout_weight --> 
    <NumberPicker android:layout_weight="0.167" 
     android:id="@+id/numberPicker3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="32dp" 
     android:descendantFocusability="blocksDescendants"/> 

    <!-- added layout_weight --> 
    <NumberPicker android:layout_weight="0.167" 
     android:id="@+id/numberPicker4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="32dp" 
     android:descendantFocusability="blocksDescendants"/> 

    <!-- added layout_weight --> 
    <NumberPicker android:layout_weight="0.167" 
     android:id="@+id/numberPicker5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="32dp" 
     android:descendantFocusability="blocksDescendants"/> 

    <!-- added layout_weight --> 
    <NumberPicker android:layout_weight="0.167" 
     android:id="@+id/numberPicker6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="32dp" 
     android:descendantFocusability="blocksDescendants"/> 
</LinearLayout> 
+1

沒問題。謝謝。 – EJK

+0

非常感謝。我的印象是它會爲我包裝所有的東西,但是我可能會在javax.swing方面考慮太多 – mike73