【问题标题】:Android - Uniform size of AlertDialog buttonsAndroid - AlertDialog 按钮的统一大小
【发布时间】:2013-03-30 22:28:58
【问题描述】:

我什至试图在 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));

【问题讨论】:

    标签: android android-alertdialog android-button


    【解决方案1】:

    最好编辑你的 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.
    

    【讨论】:

    • 我想我不明白如何处理这些或将它们放在哪里。我没有与 AlertDialog 一起使用的 XML 文件,当我创建一个时,所有按钮都只是说“False”。当我以编程方式尝试它时,我仍然只是崩溃。如何专门引用按钮以将 LayoutParams 应用于它们?
    猜你喜欢
    • 2013-01-03
    • 2011-08-14
    • 2011-12-14
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2023-03-28
    相关资源
    最近更新 更多