English 中文(简体)
正在创建自定义警告 Dialog 吗? 根视图是什么? @ info: whatsthis
原标题:Creating Custom AlertDialog ? What is the root view?
  • 时间:2012-05-22 02:42:13
  •  标签:
  • android

我想做的是:

创建自定义提醒对话框。 按钮与任何提醒对话框一样, 上面的按钮是两个文本编辑输入框。 我不想创建自定义对话框, 而是定制的提醒对话框

Here is what I am trying #3: http://developer.android.com/guide/topics/ui/dialogs.html

上面写道:

AlertDialog.Builder builder;
AlertDialog alertDialog;

Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
                           (ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");


builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();

文件指出:

View layout = inflater.inflate(R.layout.custom_dialog,
                           (ViewGroup) findViewById(R.id.layout_root));

第一个参数是布局资源代号,第二个参数是根视图的代号。

问题是我不知道布局 root 是什么? 这是一个对话框, 我要在活动中踢。 如果活动, 我是否应该使用布局代号? 布局 root 是从帽子里拔出来的吗?

还尝试:

  View layout = inflater.inflate(R.layout.my_custom_layout,
                                   (ViewGroup)   findViewById(android.R.id.content).getRootView());

结果无效指针。

问题回答

虽然这是一个老问题,但这一条对于寻找这一答案的其他人可能有用:

If you’ve ever written something like the following code using LayoutInflater in your Android application:

inflater.inflate(R.layout.my_layout, null);

PLEASE read on, because you’re doing it wrong and I want to explain to you why.

... <% 1 > BUT ...

Every Rule Has An Exception

There are of course instances where you can truly justify a null parent during inflation, but they are few. One such instance occurs when you are inflating a custom layout to be attached to an AlertDialog. Consider the following example where we want to use our same XML layout but set it as the dialog view:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
View content = LayoutInflater.from(context).inflate(R.layout.item_row, null);

builder.setTitle("My Dialog");
builder.setView(content);
builder.setPositiveButton("OK", null);
builder.show();

The issue here is that AlertDialog.Builder supports a custom view, but does not provide an implementation of setView() that takes a layout resource; so you must inflate the XML manually. However, because the result will go into the dialog, which does not expose its root view (in fact, it doesn’t exist yet), we do not have access to the eventual parent of the layout, so we cannot use it for inflation. It turns out, this is irrelevant, because AlertDialog will erase any LayoutParams on the layout anyway and replace them with match_parent.

文章解释了为什么您应该 提供父号 ViewGroup 在对话框大楼以外的大多数情况下。

Ok 。 文档中的根视图指的是自定义布局中的元素。 因此, 自定义布局将有一个叫作根视图的最外部视图。 您需要给此一个标识, 而不是您能够按显示的方式通过它。 因此, 第一个参数是自定义视图 id, 第二个参数是自定义布局元素在自定义视图中的代号 。

 View layout = inflater.inflate(R.layout.custom_dialog,
                       (ViewGroup) findViewById(R.id.layout_root));

因此,在以上文档中的示例中, R.id.layout_root 引用您给定的 id, 例如在自定义对话框布局内最外端线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性线性

你试过这个吗?

View layout = inflater.inflate(R.layout.custom_dialog,null);
builder.setView(layout);
layout.getRootView();

应该给 LinearLayout 提供 LinearLayout





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签