gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:27:20 24 4
gpt4 key购买 nike

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

喜欢这张图片:

enter image description here

此函数在 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();

}

自定义对话框.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。

关于java - 如何在对话框中创建动态布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22243522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com