Android的 - 使用自定义对话框中的一个XML布局 [英] Android - Customizing Dialog using an Xml Layout

查看:239
本文介绍了Android的 - 使用自定义对话框中的一个XML布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我trtying创建低于这个布局,但我似乎可以得到正确的
布局我需要的。我想要做到的是有一个定制dailog中,
使用布局。我试图编辑下面的XML,但如果显示为一个对话框
定义布局是一个烂摊子。请帮我明白我需要做的
这个。感谢和期待。他们Dialog正显示但布局并不满足。

此布局是什么,我试图完成的:

对话框:

 查看checkBoxView = View.inflate(这一点,R.layout.display_item_dialog,NULL);
复选框复选框=(复选框)checkBoxView.findViewById(R.id.checkBox1);
checkBox.setOnCheckedChangeListener(新OnCheckedChangeListener(){     公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
       //保存到共享preferences
     }
});
checkBox.setText(搜索所有图像);
AlertDialog.Builder建设者=新AlertDialog.Builder(本);
builder.setTitle(图像preferences);
builder.setMessage(从下方选择)
.setView(checkBoxView)
.setCancelable(假)
.setPositiveButton(是,新DialogInterface.OnClickListener(){
    公共无效的onClick(DialogInterface对话,诠释的id){
    }
})
.setNegativeButton(否,新DialogInterface.OnClickListener(){
    公共无效的onClick(DialogInterface对话,诠释的id){
        dialog.cancel();
    }
})。显示();

display_item_dialog.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:文字=地图类别:饮料/>    < ImageView的
        机器人:ID =@ + ID / imageView1
        机器人:layout_width =200dip
        机器人:layout_height =200dip
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / textView1
        机器人:layout_marginTop =14dp
        机器人:SRC =@绘制/晴天/>    <的TextView
        机器人:ID =@ + ID / textView2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_centerVertical =真
        机器人:文字=TextView的/>    <的TextView
        机器人:ID =@ + ID / textView3
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / textView2
        机器人:layout_marginTop =28dp
        机器人:文字=TextView的/>    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_alignTop =@ + ID / imageView1
        机器人:layout_marginRight =20dp
        机器人:layout_marginTop =47dp
        机器人:文字=+/>    <按钮
        机器人:ID =@ + ID /按钮2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ + ID / textView2
        机器人:layout_alignLeft =@ + ID /按钮1
        机器人:layout_marginBottom =14dp
        机器人:文本= - />    <的EditText
        机器人:ID =@ + ID / editText1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID /按钮2
        机器人:layout_alignRight =@ + ID /按钮2
        机器人:layout_below =@ + ID /按钮1
        机器人:EMS =10
        机器人:可点击=假>        < requestFocus的/>
    < /&的EditText GT;    <复选框
        机器人:ID =@ + ID / checkBox1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / textView3
        机器人:layout_marginTop =33dp
        机器人:文字=复选框/>< / RelativeLayout的>


解决方案

试试这个:

 <?XML版本=1.0编码=UTF-8&GT?;
<滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT><的LinearLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直>    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=地图类别:饮料/>    <的LinearLayout
        机器人:ID =@ + ID /线性
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginTop =14dp
        机器人:重力=center_vertical>        < ImageView的
            机器人:ID =@ + ID / imageView1
            机器人:layout_width =0dp
            机器人:layout_height =200dip
            机器人:layout_weight =0.6
            机器人:SRC =@绘制/ ic_launcher/>        <的LinearLayout
            机器人:layout_width =0dp
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =0.4
            机器人:方向=垂直>            <的TextView
                机器人:ID =@ + ID / TextView01
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文字=TextView的/>            <按钮
                机器人:ID =@ + ID /按钮1
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_gravity =CENTER_HORIZONTAL
                机器人:文字=+/>            <的EditText
                机器人:ID =@ + ID / editText1
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_gravity =CENTER_HORIZONTAL
                机器人:可点击=假>                < requestFocus的/>
            < /&的EditText GT;            <按钮
                机器人:ID =@ + ID /按钮2
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_gravity =CENTER_HORIZONTAL
                机器人:文本= - />
        < / LinearLayout中>
    < / LinearLayout中>    <的TextView
        机器人:ID =@ + ID / textView2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=TextView的/>    <的TextView
        机器人:ID =@ + ID / textView3
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=TextView的/>    <复选框
        机器人:ID =@ + ID / checkBox1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=复选框/>
< / LinearLayout中>< /滚动型>

I am trtying to create this layout below, but I can seem to get the right layout that I need. What I'm trying to accomplish is have a customize dailog box, using a layout. I tried edit the xml below but if this is shown as a dialog the defined layout is a mess. Please help me understand what I need to do for this. THanks and looking forward. They dialog is SHOWING but the layout is not met.

This layout is what I am trying to accomplish:

Dialog:

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>

解决方案

Try this:

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

这篇关于Android的 - 使用自定义对话框中的一个XML布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆