Java 如何在对话框中创建动态布局

Java 如何在对话框中创建动态布局,java,android,android-alertdialog,Java,Android,Android Alertdialog,假设我想创建一个自定义对话框 如下图所示: 此函数在onCreate(Bundle savedInstanceState)函数中调用: private void CustomDialog() { LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); final Dialog dialog =

假设我想创建一个自定义对话框

如下图所示:

此函数在onCreate(Bundle savedInstanceState)函数中调用:

private void CustomDialog() {

    LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.custom_dialog1);
    dialog.setTitle("Title");


    LinearLayout parent = (LinearLayout)findViewById(R.id.linearlayout_dialog_update);
    LinearLayout li = new LinearLayout(getApplicationContext());
    li.setOrientation(LinearLayout.HORIZONTAL);

    // imageview and textview
    ImageView image = new ImageView(getApplicationContext());
    image.setImageResource(R.drawable.ic_launcher);

    TextView text = new TextView(getApplicationContext());
    text.setText("TEST");

    li.setLayoutParams(parms);
    li.addView(image);
    li.addView(text);

    parent.addView(li);

    //header image
    ImageView resim = (ImageView) dialog.findViewById(R.id.image);
    resim.setImageResource(R.drawable.ic_launcher);
    resim.getLayoutParams().height = 220;
    resim.getLayoutParams().width = 180;
    //exit button
    Button dialogButton = (Button) dialog.findViewById(R.id.bKapat);
    dialogButton.setText("Exit");

    dialogButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();

}
自定义_dialog.xml:

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

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
     />

<LinearLayout 
    android:id="@+id/linearlayout_dialog_update"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/image"
    android:orientation="vertical"
    >

</LinearLayout>

 <Button
    android:id="@+id/bKapat"
    android:layout_width="100px"
    android:layout_height="wrap_content"
    android:text="Kapat"
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    />

</RelativeLayout>

你应该把这个从

LinearLayout parent = (LinearLayout)findViewById(R.id.linearlayout_dialog_update);


您忘了将对话框作为参考传递给我,以查找线性布局的id。

显示您的所有代码转到这个:简单计划:但他正在将此调用到onCreate()中。他的线性布局在custom_dialog.xml文件中,他在对话框中设置内容视图后找到了id。但问题是存在的,这就是原因。@PG但我认为问题不在于此。@user254039我们会来的。。很高兴帮助您!!
LinearLayout parent = (LinearLayout)findViewById(R.id.linearlayout_dialog_update);
LinearLayout parent = (LinearLayout)dialog.findViewById(R.id.linearlayout_dialog_update);