2016-03-28 42 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