【问题标题】:Adjusting size of custom dialog box in android在android中调整自定义对话框的大小
【发布时间】:2020-07-24 14:29:45
【问题描述】:

我正在为我的应用程序构建一个自定义警报对话框,我已经编写了代码及其工作,但唯一的问题是它的大小没有根据我在对话框中的内容进行调整。以下是我的代码:

public class CustomDialogBoxActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button)findViewById(R.id.Button01main);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final Dialog myDialog = new Dialog(CustomDialogBoxActivity.this);
                myDialog.setContentView(R.layout.dialog_sms_layout);
                myDialog.setTitle("Select any one");


                Button ok = (Button)myDialog.findViewById(R.id.buttonOK);
                final RadioGroup radioGrp = (RadioGroup)myDialog.findViewById(R.id.radioGroupSmsDialog);

                final CheckBox mob1 = (CheckBox)myDialog.findViewById(R.id.checkBoxMob1);
                final CheckBox mob2 = (CheckBox)myDialog.findViewById(R.id.checkBoxMob2);
                mob1.setText("9029691986");
                mob2.setText("99263911766");

                ok.setOnClickListener(this);

                ok.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        if(mob1.isChecked() && mob2.isChecked()) {
                            if(radioGrp.getCheckedRadioButtonId() == R.id.radioSms) {
                                String[] phoneArray = {mob1.getText().toString(),mob2.getText().toString()};

                                Log.v("MOB1",mob1.getText().toString());
                                Log.v("MOB2",mob2.getText().toString());
                                Log.v("Mobile No", ""+phoneArray.toString());
                                sendSms("SMS Message",phoneArray);

                            }else if(radioGrp.getCheckedRadioButtonId() == R.id.radioBusinessCard) {
                                String[] phoneArray = {mob1.getText().toString(),mob2.getText().toString()};

                                Log.v("MOB1",mob1.getText().toString());
                                Log.v("MOB2",mob2.getText().toString());
                                Log.v("Mobile No", ""+phoneArray.toString());
                                sendSms("SMS Business Card",phoneArray);
                            }
                        }else if(mob1.isChecked() && !mob2.isChecked()) {
                            if(radioGrp.getCheckedRadioButtonId() == R.id.radioSms) {
                                String[] phoneArray = {mob1.getText().toString()};

                                Log.v("MOB1",mob1.getText().toString());                                
                                Log.v("Mobile No", ""+phoneArray.toString());
                                sendSms("SMS Message",phoneArray);

                            }else if(radioGrp.getCheckedRadioButtonId() == R.id.radioBusinessCard) {
                                String[] phoneArray = {mob1.getText().toString()};

                                Log.v("MOB1",mob1.getText().toString());                                
                                Log.v("Mobile No", ""+phoneArray.toString());
                                sendSms("SMS Business Card",phoneArray);
                            }

                        }else if(!mob1.isChecked() && mob2.isChecked()) {
                            if(radioGrp.getCheckedRadioButtonId() == R.id.radioSms) {
                                String[] phoneArray = {mob2.getText().toString()};

                                Log.v("MOB2",mob2.getText().toString());
                                Log.v("Mobile No", ""+phoneArray.toString());
                                sendSms("SMS Message",phoneArray);

                            }else if(radioGrp.getCheckedRadioButtonId() == R.id.radioBusinessCard) {
                                String[] phoneArray = {mob2.getText().toString()};

                                Log.v("MOB2",mob2.getText().toString());
                                Log.v("Mobile No", ""+phoneArray.toString());
                                sendSms("SMS Business Card",phoneArray);
                            }

                        }else if(!mob1.isChecked() && !mob2.isChecked()) {
                            Toast.makeText(getApplicationContext(), "Select atleast one number", Toast.LENGTH_SHORT).show();
                        }                       
                    }
                });
                myDialog.show();
            }

        });
    }

    public void sendSms(String message,String[] phoneNumber) {

        Intent smsIntent = new Intent(Intent.ACTION_VIEW);
        smsIntent.setData(Uri.parse("sms:"));
        smsIntent.putExtra("sms_body", message);
        smsIntent.putExtra("address", phoneNumber);
        startActivity(smsIntent);       
    }
}

这是我正在点击按钮的output

这是我的 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is my main activity, from here, I want to display a dialog, 
                        after the user clicked the button below this text." />

    <Button
        android:id="@+id/Button01main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Dialog" />

</LinearLayout>

这是我的 dialog_sms_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<RadioGroup
        android:id="@+id/radioGroupSmsDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
         >

        <RadioButton
            android:id="@+id/radioSms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="SMS" />

        <RadioButton
            android:id="@+id/radioBusinessCard"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Card" />
    </RadioGroup>

<CheckBox
    android:id="@+id/checkBoxMob1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="Primary" />

<CheckBox
    android:id="@+id/checkBoxMob2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Secondary" />

<Button
    android:id="@+id/buttonOK"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="OK" />

</LinearLayout>

我想调整对话框大小,请帮忙。提前谢谢

【问题讨论】:

  • @ingsaurabh 我已经发布了我的 main.xml 以及 dialog_sms_layout.xml 并且还添加了屏幕截图的链接

标签: android


【解决方案1】:

我不确定这是否适合您的情况,但如果您想将其设置为全宽或类似的东西,您需要获取活动的当前窗口并设置它的布局参数,如下所示:

myDialog.show();
Window window = myDialog.getWindow();
window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

编辑

FILL_PARENT 已被弃用,请改用

window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

【讨论】:

  • 它可以工作,但如果我在横向模式或平板电脑中运行应用程序,那么它将占据整个屏幕,看起来很奇怪。
  • 它的宽高整数参数。只需使用类似 window.setLayout(300,300);
  • FILL_PARENT 现已被弃用!!
  • 改用 MATCH_PARENT
【解决方案2】:

您只需在对话框中添加样式“Theme_AppCompat_Light_Dialog_Alert”,如下所示:

Dialog dialog = new Dialog(context, R.style.Theme_AppCompat_Light_Dialog_Alert);

你的对话框看起来像普通的警告对话框

【讨论】:

  • 不完全是人们所要求的,但对我来说非常有效。 Tnx。
【解决方案3】:

我通过使用此代码删除标题栏解决了这个问题

    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

并为我的布局中的元素添加填充以根据我的喜好拉伸对话框。

【讨论】:

  • 完美运行,刚刚在setContentView之前写过
【解决方案4】:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:gravity="center"
    android:text="Write Feedback"
    android:textColor="@color/colorAccent"
    android:textSize="22sp"
    android:textStyle="bold" />

<RatingBar
    android:id="@+id/rating_bar"
    style="?android:attr/ratingBarStyleIndicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="8dp"
    android:isIndicator="false"
    android:numStars="5" />

<EditText
    android:id="@+id/inputSearchEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/edittextstyle"
    android:gravity="center"
    android:hint="@string/write_feedback"
    android:inputType="text"
    android:minHeight="100dp"
    android:padding="16dp" />

<Button
    android:id="@+id/submit_feedback_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="16dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/background_stroke"
    android:text="submit"
    android:textColor="@android:color/white" />
   </LinearLayout>

Dialog class :- 

dialog = new Dialog(mContext);
dialog.setContentView(R.layout.dialog_feedback);
dialog.show();

【讨论】:

  • 请关注此URL,它将有助于提升您的内容质量
【解决方案5】:

使用 Relative_Layout 而不是 Linear_layout 来获得自定义对话框的全宽。 我不知道为什么,但这对我有用。

【讨论】:

    【解决方案6】:

    好吧,我找不到解决此问题的方法,但我发现搁浅了: 创建一个 RelaytiveLayout 环绕你的根布局并将重力设置为中心,如下所示:

    <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center">
        
       <YourRootLayoutHere/>
    
    </RelativeLayout
    

    【讨论】:

      【解决方案7】:
          private void showdialog() {
          final Dialog dialog = new Dialog(this);
          dialog.setContentView(R.layout.activity_disclaimer);
          dialog.show();
          Window window = dialog.getWindow();
          window.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
          Button dialogButton = (Button) dialog.findViewById(R.id.ok);
          dialogcb= (CheckBox) dialog.findViewById(R.id.checkBox);
          dialogButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  accepted = dialogcb.isChecked();
                  SharedPreferences.Editor edit=prefs.edit();
                  edit.putBoolean("show_again",!accepted);
                  edit.commit();
                  dialog.dismiss();
              }
          });
      
          dialog.show();
      }
      

      【讨论】:

        猜你喜欢
        • 2013-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-17
        • 1970-01-01
        • 1970-01-01
        • 2013-03-13
        相关资源
        最近更新 更多