2014-03-07 74 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() –