欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義樣式圓角dialog對話框

 更新時間:2021年11月14日 15:48:58   作者:超努力的金蟬菇  
這篇文章主要為大家詳細(xì)介紹了Android自定義樣式圓角dialog對話框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android創(chuàng)建自定義樣式圓角dialog對話框的具體代碼,供大家參考,具體內(nèi)容如下

效果如上,圓角對話框,標(biāo)題和正文都可以自己設(shè)定

做法:

1.在res文件的layout文件夾創(chuàng)建自己的對話框布局,命名為my_dialog.xml
2.在res文件的drawable文件夾創(chuàng)建自己的對話框樣式(圓角),命名為my_dialog_shape.xml
3.寫一個方法調(diào)用對話框布局,觸發(fā)條件自定義,這里我是寫了一個按鈕,在按鈕的點(diǎn)擊事件里調(diào)用方法,彈出對話框。在這個方法里可以定義對話框的標(biāo)題、正文、點(diǎn)擊確定或取消時觸發(fā)的事件等,還可以設(shè)定對話框在屏幕上的顯示位置
4.在需要彈出對話框的地方調(diào)用方法

上代碼:

1.在res文件的layout文件夾創(chuàng)建自己的對話框布局,命名為my_dialog.xml

對話框內(nèi)部控件的顯示位置都可以在這里自己調(diào)整

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="16dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="@color/black"
        android:textStyle="bold"
        android:layout_marginTop="14dp"
        android:gravity="center"
        android:layout_gravity="center"
        android:id="@+id/title"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@color/black"
        android:layout_marginTop="16dp"
        android:layout_marginHorizontal="16dp"
        android:gravity="center"
        android:layout_gravity="center"
        android:id="@+id/message"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        android:layout_marginTop="16dp">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="取消"
            android:textSize="16sp"
            android:textColor="@color/white"
            android:background="@null"
            android:layout_marginRight="14dp"
            android:id="@+id/btn_cancel"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="確定"
            android:textSize="16sp"
            android:textColor="@color/white"
            android:id="@+id/btn_confirm"/>
    </LinearLayout>

</LinearLayout>

2.在res文件的drawable文件夾創(chuàng)建自己的對話框樣式(圓角),命名為my_dialog_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/white" />
    <corners android:radius="20dp"/>
</shape>

3.寫一個方法調(diào)用對話框布局,觸發(fā)條件自定義,這里我是寫了一個按鈕,在按鈕的點(diǎn)擊事件里調(diào)用方法,彈出對話框。在這個方法里可以定義對話框的標(biāo)題、正文、點(diǎn)擊確定或取消時觸發(fā)的事件等,還可以設(shè)定對話框在屏幕上的顯示位置

public void my_dialog(Context context)  {
        View inflateLayout = LayoutInflater.from(context).inflate(R.layout.my_dialog,null);
        TextView unbind_title = (TextView) inflateLayout.findViewById(R.id.title);
        unbind_title.setText("標(biāo)題");
        TextView unbind_message = (TextView) inflateLayout.findViewById(R.id.message);
        unbind_message.setText("正文");
        AlertDialog builderDialog = new AlertDialog.Builder(context)
                .setView(inflateLayout)
                .setCancelable(false)  //使用戶只能通過點(diǎn)擊對話框的確定或取消關(guān)閉對話框
                .create();

        inflateLayout.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "你點(diǎn)擊了確定", Toast.LENGTH_SHORT).show();
                builderDialog.dismiss();
            }
        });

        inflateLayout.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "你點(diǎn)擊了取消", Toast.LENGTH_SHORT).show();
                builderDialog.dismiss();
            }
        });
        builderDialog.getWindow().setBackgroundDrawableResource(R.drawable.my_dialog_shape); //設(shè)置對話框的樣式
        WindowManager.LayoutParams params = builderDialog.getWindow().getAttributes();
        params.y = 1000;
        builderDialog.getWindow().setAttributes(params);
        builderDialog.show();
        builderDialog.getWindow().setGravity(Gravity.TOP); //設(shè)置對話框展示在距離屏幕頂部1000的位置
    }

4.在需要彈出對話框的地方調(diào)用方法

例如:我在MainActivity里點(diǎn)擊了一下button,觸發(fā)了彈出對話框的方法

Button pops_up = (Button) findViewById(R.id.pops_up);
        pops_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                my_dialog(MainActivity.this);
            }
        });

代碼完整,歡迎指正

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android 控件GridView使用案例講解

    Android 控件GridView使用案例講解

    這篇文章主要介紹了Android 控件GridView使用案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • RxJava和Retrofit2的統(tǒng)一處理單個請求示例詳解

    RxJava和Retrofit2的統(tǒng)一處理單個請求示例詳解

    這篇文章主要給大家介紹了關(guān)于RxJava和Retrofit2的統(tǒng)一處理單個請求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果

    Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果

    本篇文章主要介紹了Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Android開發(fā)之圖形圖像與動畫(四)AnimationListener簡介

    Android開發(fā)之圖形圖像與動畫(四)AnimationListener簡介

    就像Button控件有監(jiān)聽器一樣,動畫效果也有監(jiān)聽器,只需要實(shí)現(xiàn)AnimationListener就可以實(shí)現(xiàn)對動畫效果的監(jiān)聽,感興趣的朋友可以了解下啊,希望本文對你有所幫助
    2013-01-01
  • Android 屏幕雙擊事件的捕獲簡單示例

    Android 屏幕雙擊事件的捕獲簡單示例

    本文主要介紹 Android屏幕雙擊事件的捕獲,這里整理了相關(guān)資料,并附示例代碼,有興趣的小伙伴可以參考下
    2016-08-08
  • Native層消息機(jī)制深入探究實(shí)例解析

    Native層消息機(jī)制深入探究實(shí)例解析

    這篇文章主要為大家介紹了Native層消息機(jī)制的深入探究及實(shí)例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android 和 windows C/C++/QT通訊時字節(jié)存儲

    Android 和 windows C/C++/QT通訊時字節(jié)存儲

    本篇文章主要介紹 Android和Windows 通訊時數(shù)據(jù)地址的理解,這里提供代碼實(shí)例進(jìn)行分析,有需要參考的朋友可以看下
    2016-07-07
  • android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放

    android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放

    本篇文章實(shí)現(xiàn)了Android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • android自定義組件實(shí)現(xiàn)方法

    android自定義組件實(shí)現(xiàn)方法

    這篇文章主要介紹了android自定義組件實(shí)現(xiàn)方法,實(shí)例分析了Android實(shí)現(xiàn)自定義組件中頁面布局及功能實(shí)現(xiàn)的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • Android開發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標(biāo)的解決方法

    Android開發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標(biāo)的解決方法

    這篇文章主要介紹了Android開發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標(biāo)的解決方法,涉及Android activity相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下
    2017-07-07

最新評論