2014-03-07 43 views
1

假设我想创建一个自定义对话框。如何在对话框中创建动态布局

这样的形象:

enter image description here

此功能在的onCreate(捆绑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) 
+0

让我看看你的所有代码 –

+0

转到此处:[http://www.mkyong.com/android/android-custom-dialog-example/](http://www.mkyong.com/android/android-定制对话框示例/) –

+0

简单计划:http://paste.ubuntu.com/7048585/ –

回答

3

您应该

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

更改为

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

你已经忘了通过对话作为参考,以找到ID为线性布局。

+0

但他调用了onCreate()。 –

+0

他的线性布局位于custom_dialog.xml文件中,他在对话框中设置了内容视图后发现了id。 – Piyush

+0

@PGsee按钮onclik() –