Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

android - How to place a table layout inside the custom dialog?

I am creating an android application which presents with custom dialog. In that custom dialog i had placed had placed a table layout generated dynamically.By executing that the dialog was showing an empty dialog with the dialog header title only it was not displaying any table layout inside that dialog can any one help me how to view dynamic table layout inside custom dialog This is my activity

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();

This is my xml file to call inside the dialog:

<?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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This can be done using setView method, other solution will be to use DialogFragment, overriding onCreateView

EDIT: int first approach you have to keep in mind that you should not call setMessage method, otherwise it will override your custom view

EDIT 2: you can look how to set custom layout for an AlertDialog in this developer page


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...