自定义android dialog

来源:互联网 发布:襄阳樊城区移动云计算 编辑:程序博客网 时间:2024/05/18 02:26

以下例子为使用自定义页面布局来制作自定义dialog,下面分别展示java代码和xml布局代码:


java dialog代码:

public void showTextQuestionDialog(String title, String content, final int position)//dialog方法
{


final AlertDialog build = new AlertDialog.Builder(this).create();
build.show();


Window window = build.getWindow();
window.setContentView(R.layout.test_question);//创建对话框使用到的布局页面


build.setCancelable(false);//对话框点击其他位置是否可取消


ImageButton close;


final RadioButton yes;

final RadioButton no;

//所在布局页面的控件

TextView text_question1_content_txt = (TextView) window.findViewById(R.id.text_question1_content_txt);
TextView text_question1_title_tv = (TextView) window.findViewById(R.id.text_question1_title_tv);
text_question1_title_tv.setText(title);
text_question1_content_txt.setText(content);
close = (ImageButton) window.findViewById(R.id.text_question_close);
// share = (ImageButton)
// window.findViewById(R.id.text_question1_share_imgbuton);
yes = (RadioButton) window.findViewById(R.id.text_question1_yes);
no = (RadioButton) window.findViewById(R.id.text_question1_no);


布局文件xml代码:

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


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="320dp"
        android:layout_centerInParent="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/text_question_bg" >


        <TextView
            android:id="@+id/text_question1_title_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="测试题1"
            android:textColor="@android:color/white"
            android:textSize="20dp"
            android:layout_marginTop="10dp" />
        
        <ImageView 
            android:id="@+id/text_question1_content_img"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_below="@id/text_question1_title_tv"
            android:background="@drawable/text_question1_content"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:visibility="gone"
            android:layout_marginTop="40dp"
            />
        <TextView 
            android:id="@+id/text_question1_content_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/text_question1_title_tv"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="40dp"
            />
        
        <RadioGroup 
            android:id="@+id/text_question1_select"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="50dp"
            android:layout_marginTop="30dp"
            android:layout_below="@id/text_question1_content_txt"
            android:orientation="horizontal">




            <RadioButton 
                android:id="@+id/text_question1_yes"
                android:layout_width="70dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:button="@null"
                android:background="@drawable/test_question_radio_yes_style"/>
            
            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_weight="2"/>


            <RadioButton 
                android:id="@+id/text_question1_no"
                android:layout_width="70dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:button="@null"
                android:background="@drawable/test_question_radio_no_style"/>


        </RadioGroup>
        
       
        
    </RelativeLayout>


    <ImageButton
        android:id="@+id/text_question_close"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:layout_alignParentRight="true"
        android:background="@drawable/text_question_close"
        android:layout_marginBottom="30dp" />


</RelativeLayout>

显示页面如下:


0 0