Android:警报对话框标题的位置低于该行

时间:2018-10-14 13:41:52

标签: android android-alertdialog

单击列表视图中的一项时,我将显示一个警报对话框。一切都很好,除了对话框标题的位置错误,如下图所示。为什么标题位于行下方而不是行上方?

enter image description here

警报对话框的代码

ListView listView = (ListView)findViewById(R.id.listView);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,
                android.R.style.Theme_Holo_Light_Dialog);
        View view2 = getLayoutInflater().inflate(R.layout.dialog,null);
        builder.setView(view2);
        builder.setTitle("Edit")
                .setCancelable(false)
                // Add action buttons
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        //
                        refreshData();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                    }
                })
                .setIcon(android.R.drawable.ic_menu_edit)
                .show();
    }
});

警报对话框的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <EditText
        android:id="@+id/edittext_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Date"
        android:inputType="text" />

    <EditText
        android:id="@+id/edittext_point"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Point"
        android:inputType="numberSigned" />

    <EditText
        android:id="@+id/edittext_description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Description"
        android:inputType="text" />

</LinearLayout>     

1 个答案:

答案 0 :(得分:0)

您在对话框中使用了不推荐使用的样式。试试这个:

final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

这对我来说很好。


编辑

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->  
    ...
    <item name="alertDialogTheme">@style/AlertDialogStyle</item> 
    ...
</style>
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">#c0ff00</item>
    <item name="android:titleTextStyle">@style/MyTextStyle</item>
</style>