2011-05-03 70 views
3

编辑:经验教训:设置方向或可能不显示的东西。多个视图不会显示在自定义警报对话框中

按照http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog我试图创建一个自定义提醒对话框。具体来说,我创建了一个包含多个视图(称为HelpItemView)的对话框,每个视图都包含两个文本视图。我正在使用它创建一个警报对话框,其中包含标题内容对形式的帮助信息。

我的问题是只有第一个HelpItemView出现,尽管我正在创建一堆。警报对话框中是否有限制此操作的内容?我搞砸了我的代码,并没有任何问题,当他们连接到我的活动的主要线性布局时显示所有的HelpItemViews,似乎这个问题只出现在警报对话框。我的活动范围内

代码:

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,如果这有什么区别。我愿意转换为一个listview,如果这可以更好地工作(或根本),但我仍然想知道当前代码有什么问题,如果任何人都可以弄明白的话。

回答

3

我认为你应该改变helpGroup的方向为横向。

+0

真的吗?哦,我不能相信它,完全有效......多么痛苦。我不知道没有设定方向可能会产生如此严重的后果。永远的谢意。 – Emily 2011-05-03 05:58:22

+0

默认情况下,LinearLayout的方向是水平的。在Android中使用错误的方向是非常常见的问题。不客气! – Michael 2011-05-03 06:21:26