【问题标题】:Android problem: Custom dialog layout is used instead of main.xml for root activityAndroid 问题:根活动使用自定义对话框布局而不是 main.xml
【发布时间】:2010-07-20 20:59:57
【问题描述】:

我正在关注this guide 来制作自定义警报对话框。我想制作一个带有按钮和标题的对话框,如 AlertDialog,但也有一个 EditText 框,这是常规 AlertDialog 无法使用的。因此,我在 res/layout 中名为 add_player_dialog.xml 的文件中制作了以下自定义布局,其中包含 EditText 框,该文件将由 AlertDialog.Builder 的 setView() 调用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/add_player_dialog_layout"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="10dp"
   >
  <EditText
     android:id="@+id/player_name"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background="@android:drawable/editbox_background"
     android:textSize="20sp"
     />
</LinearLayout>

然后在启动对话框的活动(不是根活动,顺便说一句)中,我在 onCreateDialog() 的开关中有以下内容:

    Context mContext = getApplicationContext();
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.add_player_dialog,
                   (ViewGroup) findViewById(R.id.add_player_dialog_layout));

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Enter player's name")
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Toast.makeText(getApplicationContext(), "yes", Toast.LENGTH_SHORT).show();
        }
        })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
        })
    .setView(layout);
    AlertDialog alert = builder.create();

问题在于应用程序启动时首先使用的活动使用 add_player_dialog.xml 进行布局,而不是 main.xml。我该如何避免这种情况?

==================

编辑:如果有人感兴趣,我已经找到了一种编程方式来实现这一点:

import android.R.drawable;

EditText et = new EditText(this);
et.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
et.setBackgroundResource(drawable.editbox_background);
et.setTextSize(20);

==================

如果你有兴趣,这里是 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="40dip"
   >

  <LinearLayout
     android:orientation="vertical"
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:layout_gravity="center"
     >
    <Button
       android:id="@+id/new_game_button"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_gravity="center"
       android:text="@string/new_game_button"
       />
    <Button
       android:id="@+id/continue_button"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_gravity="center"
       android:text="@string/continue_button"
       />
  </LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: android customdialog


    【解决方案1】:

    你尝试过/有过

    setContentView(R.layout.main);
    

    在 onCreate 方法中?

    如果是这样,对不起,希望其他人能更好地加入。

    【讨论】:

    • 是的,我有。谢谢你的帮助。
    • 你能发布你的 main.xml 和你谈论的活动吗?
    【解决方案2】:

    尝试删除 gen/R.java 并让构建系统重新生成它,我发现有时当您添加 ID 时,它会不同步,需要强制重新生成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多