android - 在android中调整自定义对话框的大小

标签 android

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

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>

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

最佳答案

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

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

编辑

FILL_PARENT 已被弃用,请改用

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

关于android - 在android中调整自定义对话框的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10242144/

相关文章:

android - GLSurfaceView - 如何制作半透明背景

android - 混淆文件夹结构 - Android

java - 多个嵌套布局字段中的事件 (OnClickListener)

Android加速度计采样率统计

android - 错误:Execution failed for task ':app:transformClassesWithDexForDebug' .没有解决

java - Activity 和 Intent

java - 通过 Intent 的对象数组列表

java - 从 Android/Java 应用程序 POSTING 到 PHP + MYSQL 时出现问题

java - 在 android 上使用 java-google-translate-text-to-speech

android - Volley 中的软过期有什么效果?