Android - AlertDialog按钮的统一大小

时间:2013-03-30 22:28:58

标签: android android-alertdialog android-button

我正试图在AlertDialog中获取我的按钮。目前,一个按钮的文本进入第二行,使该按钮略大于另外两个按钮。这是我的对话框(不在片段中,只在活动中)

public void imageActions(final View v, final int position) {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Image Actions");
    alert.setIcon(R.drawable.image_icon);
    alert.setNeutralButton("View Image", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
              seeImage(v,position);
        } });   
    alert.setPositiveButton("Remove Image", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
              sureToDelte(v,position);
        } });       
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {  
        } }); 
    alert.show();           
}

我尝试在alert.show();之后添加此代码但导致崩溃。

Button positive = (Button) findViewById(android.R.id.button1);
positive.setLayoutParams(new LinearLayout.LayoutParams(10, 10));
Button negative = (Button) findViewById(android.R.id.button2);
negative.setLayoutParams(new LinearLayout.LayoutParams(10, 10));
Button neutral = (Button) findViewById(android.R.id.button3);
neutral.setLayoutParams(new LinearLayout.LayoutParams(10, 10));

1 个答案:

答案 0 :(得分:0)

最好编辑XML文件,你可以这样做:

<LinearLayout
        android:id="@+id/activity_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

            <Button
                android:id="@+id/buttonID1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Your Button" />
            <Button
                android:id="@+id/buttonID2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Your Button" />
</LinearLayout>

否则,如果您真的想以编程方式编辑它,请使用LinearLayout属性,如:

LinearLayout layout = (LinearLayout) findViewById(R.id.your_linear);
LayoutParams params = layout.getLayoutParams(); // Params
params.width = params.FILL_PARENT; // Fill the space horizontally
params.height = params.WRAP_CONTENT; // Fill the space vertically but "automatically", depends on the object's size.