【问题标题】:How to place a table layout inside the custom dialog?如何在自定义对话框中放置表格布局?
【发布时间】:2015-07-01 16:33:07
【问题描述】:

我正在创建一个带有自定义对话框的 android 应用程序。在我放置的那个自定义对话框中放置了一个动态生成的表格布局。通过执行该对话框显示一个带有对话框标题标题的空对话框,它没有在该对话框中显示任何表格布局,任何人都可以帮助我如何查看动态自定义对话框内的表格布局 这是我的活动

alert_progress_dialog = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
alert_progress_dialog.setTitle("MANUAL MODE : TESTING ");
View dialogview = inflater.inflate(R.layout.progressdialog, null);
alert_progress_dialog.setView(dialogview);
alert_progress_dialog.setMessage("This is a sample message");
table_dialog = (TableLayout)dialogview.findViewById(R.id.table_layout_1);

for (int i = 1; i <= 4; i++) {

    TableRow row = new TableRow(getActivity());
    row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));


    for (int j = 1; j <= 4; j++) 
    {
        TextView tv = new TextView(getActivity());
        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        tv.setBackgroundResource(R.drawable.cell_shape);
        tv.setPadding(5, 5, 5, 5);
        tv.setText("R " + i + ", C" + j);
        row.addView(tv);
    }

    table_dialog.addView(row);
}
alert_progress_dialog.show();

这是我要在对话框中调用的 xml 文件:

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

    <TableLayout
        android:id="@+id/table_layout_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="77dp" >
    </TableLayout>

</RelativeLayout>

【问题讨论】:

标签: android android-alertdialog


【解决方案1】:

这可以使用setView 方法完成,其他解决方案是使用DialogFragment,覆盖onCreateView

编辑:int first 方法你必须记住你不应该调用 setMessage 方法,否则它会覆盖你的自定义视图

编辑 2:您可以在 developer page 中查看如何为 AlertDialog 设置自定义布局

【讨论】:

  • 对不起,伙计,我没听懂你能不能再重复一遍我是这个@Chaosit的新手
  • @BujjiDeepu 你可以看看我在第二次编辑中添加的链接
  • 它不起作用,伙计,我已经放置了 setView() 方法,我也尝试了你的第二次编辑,但它不起作用,伙计 @Chaosit
【解决方案2】:

使用 Dialog 而不是 AlertDialog,然后将 setContentView 与您的布局 ID 一起使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多