如何让自定义警告对话框,像图像显示的? [英] how to get customized alert dialog , like the one shown in image?

查看:116
本文介绍了如何让自定义警告对话框,像图像显示的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个自定义对话框,就像图片中所示的

我已经创建了两个版式一为自定义标题和放大器;以及两个buttons.Here其他含两个编辑文本视图是XML的

custom_title

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直
    机器人:背景=@可绘制/ popup_title
    机器人:重力=center_vertical>

    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/ addView
        机器人:文字颜色=@彩色/黑白

        机器人:以下属性来=15dp/>

< / LinearLayout中>
 

custom_dialog.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:背景=@色/白>

    <的EditText
        机器人:ID =@ + ID / textView1
        机器人:layout_width =0dp
        机器人:layout_height =WRAP_CONTENT
        机器人:提示=@字符串/标题
        机器人:layout_weight =1/>

    <的EditText
        机器人:ID =@ + ID / textView2
        机器人:layout_width =0dp
        机器人:layout_height =WRAP_CONTENT
        机器人:提示=@字符串/详细信息
        机器人:layout_weight =1/>

    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT>

        <按钮
            机器人:ID =@ + ID /按钮1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=@可绘制/ btn_add
            机器人:layout_weight =1/>

        <按钮
            机器人:ID =@ + ID /按钮2
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=@可绘制/ btn_cancel
            机器人:layout_weight =1/>

    < / LinearLayout中>

< / LinearLayout中>
 

解决方案

您只需要膨胀对话与你的XML文件。 我只是给你一个样本code,那么你可以很容易地跟踪

 私人查看MView的;
私人对话mDialog;
私人LayoutInflater mInflater;
 

现在创建一个功能: -

 私人无效showCustomDialog(){
    mInflater =(LayoutInflater)getBaseContext()。getSystemService(
            LAYOUT_INFLATER_SERVICE);
    ContextThemeWrapper mTheme =新ContextThemeWrapper(这一点,
            R.style.YOUR_STYE);

    MVIEW = mInflater.inflate(R.layout.YOUR_XML_LAYOUT_FILE,NULL);
    // mDialog =新的对话框(此,0); //背景下,主题

    mDialog =新的对话框(mTheme);
    mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    mDialog.setContentView(this.mView);
    mDialog.show();

    TextViiew someText =(TextView中)mView.findViewById(R.id.textViewID);
    //做一些事情,与文本视图或任何其他视图

}
 

最后,确保你有风格: -

 <样式名称=YOUR_STYLE>
    <项目名称=机器人:windowBackground> @android:彩色/透明< /项目>
    <项目名称=机器人:窗框> @空< /项目>
    <项目名称=机器人:windowNoTitle>真< /项目>
    <项目名称=机器人:windowIsFloating>假< /项目>
    <项目名称=机器人:windowIsTranslucent>假< /项目>
    <项目名称=机器人:windowContentOverlay> @android:彩色/透明< /项目>
    <项目名称=机器人:backgroundDimEnabled>假< /项目>
< /风格>
 

这是它....你完成......只是调用这个函数在任何你想显示自定义对话框......

希望的解释是有用。

i want to build a customized dialog just like the one shown in image:

i have created two layout one for custom title & other containing two edit text view along with two buttons.Here are xml's

custom_title

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:background="@drawable/popup_title"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/addView"
        android:textColor="@color/black" 

        android:paddingLeft="15dp"/>

</LinearLayout>

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/white" >

    <EditText
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/title"
        android:layout_weight="1" />

    <EditText
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/details"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_add" 
            android:layout_weight="1"/>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_cancel" 
            android:layout_weight="1"/>

    </LinearLayout>

</LinearLayout>

解决方案

You just have to inflate the Dialog with your xml file. I'll just give you a sample code, then you can easily follow

private View mView;
private Dialog mDialog;
private LayoutInflater mInflater;

Now create a function as :-

private void showCustomDialog() {
    mInflater = (LayoutInflater) getBaseContext().getSystemService(
            LAYOUT_INFLATER_SERVICE);
    ContextThemeWrapper mTheme = new ContextThemeWrapper(this,
            R.style.YOUR_STYE);

    mView = mInflater.inflate(R.layout.YOUR_XML_LAYOUT_FILE, null);
    // mDialog = new Dialog(this,0); // context, theme

    mDialog = new Dialog(mTheme);
    mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    mDialog.setContentView(this.mView);
    mDialog.show();

    TextViiew someText = (TextView) mView.findViewById(R.id.textViewID);
    // do some thing with the text view or any other view 

}

Finally make sure you have the style as :-

<style name="YOUR_STYLE">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowContentOverlay">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

That's it .... you are done... just call this function where ever you want to show the custom dialog....

Hope the explanation was useful....

这篇关于如何让自定义警告对话框,像图像显示的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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