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

Android自定義樣式圓角dialog對(duì)話框

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

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

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

做法:

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

上代碼:

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

對(duì)話框內(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)建自己的對(duì)話框樣式(圓角),命名為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.寫(xiě)一個(gè)方法調(diào)用對(duì)話框布局,觸發(fā)條件自定義,這里我是寫(xiě)了一個(gè)按鈕,在按鈕的點(diǎn)擊事件里調(diào)用方法,彈出對(duì)話框。在這個(gè)方法里可以定義對(duì)話框的標(biāo)題、正文、點(diǎn)擊確定或取消時(shí)觸發(fā)的事件等,還可以設(shè)定對(duì)話框在屏幕上的顯示位置

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)  //使用戶只能通過(guò)點(diǎn)擊對(duì)話框的確定或取消關(guān)閉對(duì)話框
                .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è)置對(duì)話框的樣式
        WindowManager.LayoutParams params = builderDialog.getWindow().getAttributes();
        params.y = 1000;
        builderDialog.getWindow().setAttributes(params);
        builderDialog.show();
        builderDialog.getWindow().setGravity(Gravity.TOP); //設(shè)置對(duì)話框展示在距離屏幕頂部1000的位置
    }

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

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

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);
            }
        });

代碼完整,歡迎指正

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

相關(guān)文章

最新評(píng)論