Android - 使用XML布局自定义对话框

8
我正在尝试创建下面的布局,但似乎无法得到我需要的正确布局。我的目标是使用自定义布局来创建一个对话框框。我尝试编辑下面的XML,但如果将其显示为对话框,则定义的布局会混乱。请帮助我理解我需要做什么。感谢并期待回复。对话框已经显示,但布局没有达到预期。
这个布局就是我想要实现的:
对话框:
View checkBoxView = View.inflate(this, R.layout.display_item_dialog, null);
CheckBox checkBox = (CheckBox)checkBoxView.findViewById(R.id.checkBox1);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       // Save to shared preferences
     }   
});
checkBox.setText("Search All Images");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Image Preferences");
builder.setMessage(" Select from below ")
.setView(checkBoxView)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
    }
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
    }
}).show();

display_item_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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Map Category: BEVERAGES" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="200dip"
        android:layout_height="200dip"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="14dp"
        android:src="@drawable/sunny" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="28dp"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/imageView1"
        android:layout_marginRight="20dp"
        android:layout_marginTop="47dp"
        android:text="+" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignLeft="@+id/button1"
        android:layout_marginBottom="14dp"
        android:text="-" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignRight="@+id/button2"
        android:layout_below="@+id/button1"
        android:ems="10"
        android:clickable="false" >

        <requestFocus />
    </EditText>

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="33dp"
        android:text="CheckBox" />

</RelativeLayout>
2个回答

9
你可以参考这个教程: http://www.mkyong.com/android/android-custom-dialog-example/ 它与Android的自定义对话框有关。
        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("Title...");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

你好!我已经添加了所需的布局。现在我有点担心所需的XML布局,Nirali先生。 - rahstame
你的意思是无法按照图片所示设置XML视图吗? - Nirali
上面的图片是我需要在显示对话框中拥有的图片,或者对话框应该看起来与上面的图片相同。我需要使用XML进行工作。 - rahstame
为什么官方的Android文档会说.. "Dialog类是对话框的基类,但您应避免直接实例化Dialog。相反,请使用以下子类之一.." [https://developer.android.com/guide/topics/ui/dialogs.html] ??? - Redoman

5

试试这个:

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

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Map Category: BEVERAGES" />

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:gravity="center_vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="0dp"
            android:layout_height="200dip"
            android:layout_weight="0.6"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/TextView01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="+" />

            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:clickable="false" >

                <requestFocus />
            </EditText>

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="-" />
        </LinearLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />
</LinearLayout>

</ScrollView>

1
如果仍然存在问题,则将整个布局放入 ScrollView 中。 - M-Wajeeh
先生,正如您建议使用ScrollView,但它并没有起作用。您能否检查一下在这个布局中是否可以使用ScrollView? - rahstame
解释一下“不起作用”的原因。我已经将它放在了“ScrollView”中,请现在检查答案。 - M-Wajeeh
如果在XML中定义了点击事件,如何获取它? - Ruchir Baronia
你必须在Java代码中设置点击监听器,据我所知,android:onClick仅适用于活动。 - M-Wajeeh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接