繁体   English   中英

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

[英]how to create dynamic layout inside a 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();

}

custom_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>

我收到这些错误:

 03-07 10:18:37.604: E/AndroidRuntime(489): FATAL EXCEPTION: main 03-07
 10:18:37.604: E/AndroidRuntime(489): java.lang.NullPointerException
 03-07 10:18:37.604: E/AndroidRuntime(489):     at
 com.example.ex78.MainActivity.CustomDialog(MainActivity.java:66) 03-07
 10:18:37.604: E/AndroidRuntime(489):   at
 com.example.ex78.MainActivity.onClick(MainActivity.java:32) 03-07
 10:18:37.604: E/AndroidRuntime(489):   at
 android.view.View.performClick(View.java:3110) 03-07 10:18:37.604:
 E/AndroidRuntime(489):     at
 android.view.View$PerformClick.run(View.java:11934) 03-07
 10:18:37.604: E/AndroidRuntime(489):   at
 android.os.Handler.handleCallback(Handler.java:587) 03-07
 10:18:37.604: E/AndroidRuntime(489):   at
 android.os.Handler.dispatchMessage(Handler.java:92) 03-07
 10:18:37.604: E/AndroidRuntime(489):   at
 android.os.Looper.loop(Looper.java:132) 03-07 10:18:37.604:
 E/AndroidRuntime(489):     at
 android.app.ActivityThread.main(ActivityThread.java:4123) 03-07
 10:18:37.604: E/AndroidRuntime(489):   at
 java.lang.reflect.Method.invokeNative(Native Method) 03-07
 10:18:37.604: E/AndroidRuntime(489):   at
 java.lang.reflect.Method.invoke(Method.java:491) 03-07 10:18:37.604:
 E/AndroidRuntime(489):     at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
 03-07 10:18:37.604: E/AndroidRuntime(489):     at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 03-07
 10:18:37.604: E/AndroidRuntime(489):   at
 dalvik.system.NativeStart.main(Native Method)

你应该改变这个

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

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

您忘记将对话框作为参考来查找线性布局的ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM