【问题标题】:AlertDialog with custom layout not showing up具有自定义布局的 AlertDialog 未显示
【发布时间】:2017-12-03 21:45:02
【问题描述】:

我正在尝试使用自定义视图布局创建一个警报对话框,但屏幕只是变暗,没有出现任何对话框。

AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setView(findViewById(R.id.system_profile_dialog));
AlertDialog setSysProfileDialog = builder.create();
setSysProfileDialog.show();

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/system_profile_dialog"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:hint="@string/system_profile_hint"
        android:inputType="number" />
</LinearLayout>

【问题讨论】:

    标签: java android android-studio dialog android-alertdialog


    【解决方案1】:

    问题出在这一行:

    builder.setView(findViewById(R.id.system_profile_dialog));
    

    改成:

    builder.setView(LayoutInflater.from(this).inflate(R.layout.layout_name, null, false));
    

    您应该在那里传递一个膨胀的视图,而不是当前活动中视图的id

    【讨论】:

      【解决方案2】:

      试试看

      Dialog dialog = new Dialog(this);
              dialog.setContentView(R.layout.YourFile);
              EditText editText = (EditText)dialog.findViewById(R.id.YourEditText);  
              dialog.show();
      

      【讨论】:

        【解决方案3】:

        尝试使用实际的 Context 对象实例化构建器,调用 getApplicationContext()

        【讨论】:

          【解决方案4】:

          只需替换 builder.setView(findViewById(R.id.system_profile_dialog));使用 builder.setView(R.id.system_profile_dialog)。如果您查看官方文档,您会发现 setView(View) 只是设置要显示的视图,但 setView(int layoutResourceId) 也在膨胀视图。

          【讨论】:

          • 是的,你是对的。如果你的目标是
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多