如何获得自定义警报对话框,如图所示?

2024-04-10

want to have alert dialog like thisi want to build a customized dialog just like the one shown in image:

我创建了两个布局,一个用于自定义标题,另一个包含两个编辑文本视图和两个按钮。这里是 xml

自定义标题

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

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

您只需使用 xml 文件来扩展对话框即可。 我只会给您一个示例代码,然后您就可以轻松地遵循

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

现在创建一个函数:-

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 

}

最后确保你的风格是:-

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

就是这样......你已经完成了......只需在你想要显示自定义对话框的地方调用这个函数......

希望解释有用......

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获得自定义警报对话框,如图所示? 的相关文章

随机推荐