自定义警告对话框中未显示多个视图

3

编辑:教训:设置方向,否则可能会导致内容无法显示。

根据http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog,我正在尝试创建一个自定义警报对话框。具体而言,我正在创建一个对话框,其中包含多个视图(称为HelpItemView),每个视图都包含两个文本视图。我使用这个来创建一个警报对话框,其中包含以标题-内容对的形式呈现的帮助信息。

我的问题是,尽管我创建了很多HelpItemView,但只有第一个HelpItemView出现了。是否有什么东西在警报对话框中限制了这一点?当它们附加到我的活动的主线性布局时,我调整了我的代码,并没有遇到任何问题,似乎这个问题只出现在警报对话框中。

我的活动内部代码:

private void createHelp() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    Resources res = getResources();
    LinearLayout help_root = (LinearLayout)getLayoutInflater().inflate(R.layout.help_frame, null);
    LinearLayout help = (LinearLayout) help_root.findViewById(R.id.helpGroup);
    help.addView(new HelpItemView(this, res.getString(R.string.help_main_welcome), 
            res.getString(R.string.help_main_welcome_content)));
    help.addView(new HelpItemView(this, res.getString(R.string.add_id), 
            res.getString(R.string.help_add_id_content)));
    help.addView(new HelpItemView(this, res.getString(R.string.view_ids), 
            res.getString(R.string.help_view_ids_content)));
    help.addView(new HelpItemView(this, res.getString(R.string.view_map), 
            res.getString(R.string.help_view_map_content)));
    help.addView(new HelpItemView(this, res.getString(R.string.browse_plants), 
            res.getString(R.string.help_browse_plants_content)));
    help.addView(new HelpItemView(this, res.getString(R.string.btnQuickIdentification), 
            res.getString(R.string.help_btnQuickIdentification_content)));
    help.addView(new HelpItemView(this, res.getString(R.string.btnOptions), 
            res.getString(R.string.help_options_content)));
    builder.setView(help_root);
    AlertDialog alert = builder.create();
    alert.show();   
}

helpframe.xml

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

HelpItemView.java

public class HelpItemView extends RelativeLayout {
private TextView m_vwItemTitle;
private TextView m_vwItemText;

public HelpItemView (Context context, String header, String content) {
    super(context);
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.help_item, this, true);
    m_vwItemTitle = (TextView) this.findViewById(R.id.helpItemHeader);
    m_vwItemText = (TextView) this.findViewById(R.id.helpItemText);
    setContent(header, content);
}

private void setContent(String header, String content) {
    m_vwItemTitle.setText(header);
    m_vwItemText.setText(content);
}

}

help_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/helpItemHeader"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/helpItemText"
/>
</LinearLayout>

也许这只是一些愚蠢的问题我无法理解,我不知道。如果您有任何想法/帮助,将不胜感激。哦,如果有什么区别,我正在开发Android 1.6。如果可以更好地使用列表视图(或完全),我愿意进行转换,但如果有人能找出当前代码的问题,我仍然想知道是什么。
1个回答

3
我认为你应该将helpGroup的方向改为水平方向。

真的吗?哦,我简直不敢相信,完全有效...太痛苦了。我从没想过不设置方向会有如此严重的后果。永远感谢。 - Emily
默认情况下,LinearLayout的方向是水平的。在Android中,使用错误的方向是非常常见的问题。欢迎您! - Michael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接