簡體   English   中英

調整android中自定義對話框的大小

[英]Adjusting size of custom dialog box in 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);       
    }
}

這是我點擊按鈕時的輸出

這是我的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>

我想調整對話框大小請幫忙。 提前致謝

我不確定這是否適用於您的情況,但如果您想將其設置為全寬或類似的東西,您需要獲取活動的當前窗口並設置它的布局參數如下:

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

編輯

FILL_PARENT已被棄用,請改用

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

您只需要在對話框中添加樣式“Theme_AppCompat_Light_Dialog_Alert”,如下所示:

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

並且您的對話框看起來像普通警報對話框

我通過使用此代碼刪除標題欄解決了這個問題

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

並添加填充到我的布局中的元素,以擴展我喜歡的對話框。

<?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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM