Android布局 - 按钮宽度

时间:2014-01-16 12:55:27

标签: android android-layout

我目前创建了一个简单的自定义对话框片段,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#202020"
    android:orientation="vertical"
    android:gravity="center" >

    <TextView
        android:padding="10dp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/custom_confirmation_dialog_title"
        android:background="@drawable/title_background"
        android:gravity="center"
        android:text="Title"
        android:textColor="#EEEEEE" />

    <TextView
        android:padding="10dp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/custom_confirmation_dialog_message"
        android:text="Message goes here!"
        android:textColor="#EEEEEE" />

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

        <Button
            android:id="@+id/custom_confirmation_dialog_positive_button"
            android:text="Text for button 1"
            android:layout_height="fill_parent"
            android:layout_width="0px" 
            android:layout_weight="1"
            android:singleLine="true" />

        <Button
            android:id="@+id/custom_confirmation_dialog_negative_button"
            android:text="Longer text for button 2"
            android:layout_height="fill_parent"
            android:layout_width="0px" 
            android:layout_weight="1"
            android:singleLine="true" />

    </LinearLayout>

</LinearLayout>

结合以下Java代码:

class CustomConfirmationDialog extends DialogFragment

{
 public CustomConfirmationDialog() {}


 public static CustomConfirmationDialog newInstance()
 {
  CustomConfirmationDialog customConfirmationDialog =
    new CustomConfirmationDialog();
  customConfirmationDialog.setCancelable(false);
  return customConfirmationDialog;
 }



 @Override public Dialog onCreateDialog(Bundle savedInstanceState)
 {
  final Dialog dialog = new Dialog(getActivity());
  dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
  dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                              WindowManager.LayoutParams.FLAG_FULLSCREEN);
  dialog.setContentView(R.layout.custom_confirmation_dialog);
  return dialog;
 }


};

问题是,即使有足够的可用屏幕空间,对话框似乎也不希望扩展到足以容纳第二个按钮的文本。 有谁知道如何实现我正在寻找的东西?

1 个答案:

答案 0 :(得分:0)

试试这个..

<Button
            android:id="@+id/custom_confirmation_dialog_positive_button"
            android:text="Text for button 1"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content" 
            android:layout_weight="1"
            android:singleLine="true" />

        <Button
            android:id="@+id/custom_confirmation_dialog_negative_button"
            android:text="Longer text for button 2"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content" 
            android:layout_weight="1"
            android:singleLine="true" />