Android对话框没有填充

时间:2016-06-29 15:03:04

标签: android dialog

我有一个AlertDialog的问题 - 对话框没有填充(如截图 - 内部没有填充的对话框)。

我的活动主题为“NoActionBar”

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

我的活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDial();
        }
    });
}

private void showDial() {
    new AlertDialog.Builder(this)
            .setTitle("Delete entry")
            .setMessage("Are you sure you want to delete this entry?")
            .setPositiveButton("tak", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // continue with delete
                }
            })
            .show();
}

有谁知道如何修复它?

2 个答案:

答案 0 :(得分:0)

您必须在对话框提醒中使用自定义视图

private void showDial() {

    View content = this.getLayoutInflater().inflate(R.layout.custom_dialog_alert, null);
    ((TextView)content.findViewById(R.id.dialogTitle)).setText("Delete entry");
    ((TextView)content.findViewById(R.id.dialogText)).setText("Are you sure you want to delete this entry?");


    AlertDialog alert = new AlertDialog.Builder(Test.this, R.style.no_title_dialog)
            .setCancelable(true)
            .setPositiveButton("tak", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // continue with delete
                }
            })
            .setView(content)
            .create();
    alert.show();
}

custom_dialog_alert.xml

<?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"
android:background="@android:color/holo_red_light">

<TextView
    android:id="@+id/dialogTitle"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:paddingTop="10dp"
    android:ellipsize="end"
    android:gravity="left"
    android:lines="1"
    android:paddingLeft="26dp"
    android:paddingRight="10dp"
    android:text="Dialog title"
    android:textColor="@android:color/black"
    android:background="@android:color/background_light"
    android:textSize="20sp" />

<RelativeLayout
    android:id="@+id/contentText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/background_light"
    android:minHeight="40dp"
    android:paddingTop="10dp">

        <TextView
            android:id="@+id/dialogText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:gravity="left"
            android:maxHeight="300dp"
            android:text="Dialog text"
            />
</RelativeLayout>

styles.xml中的

no_title_dialog

<style name="no_title_dialog" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:windowNoTitle">true</item>
</style>

我希望它有所帮助

答案 1 :(得分:0)

将此样式添加到对话框中。

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(StartUpActivity.this
                ,R.style.AlertDialogCustom));


<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:fitsSystemWindows">false</item>
    </style>