Android 好看的底部弹出UI对话框

应用选择对话框的实现

1. 效果预览动图

Android 好看的底部弹出UI对话框_第1张图片

实现代码:github

代码在包名:programselectdialog中;


2.实现思路

	1.布局使用的是ConstraintLayout
	2.应用选择实现是去继承Dialog
	3.在类中暴露出接口,获取点击的信息

3.代码


3.1 select_dialog.xml //对话框UI样式

xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_back">
    android:id="@+id/tv_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:textSize="16sp"
        android:textColor="#000"
        android:text="应用选择"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    android:id="@+id/iv_doc_qq"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:contentDescription="qq图片"
        android:src="@mipmap/qq"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/iv_doc_phone"
        app:layout_constraintTop_toBottomOf="@+id/tv_add"
        app:layout_constraintHorizontal_weight="1"/>
    android:id="@+id/iv_doc_phone"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:contentDescription="手机图片"
        android:src="@mipmap/myphone"
        app:layout_constraintTop_toTopOf="@+id/iv_doc_qq"
        app:layout_constraintLeft_toRightOf="@+id/iv_doc_qq"
        app:layout_constraintRight_toLeftOf="@+id/iv_doc_wx"
        app:layout_constraintHorizontal_weight="1"/>
    android:id="@+id/iv_doc_wx"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:contentDescription="微信图片"
        android:src="@mipmap/wx"
        app:layout_constraintTop_toTopOf="@+id/iv_doc_phone"
        app:layout_constraintLeft_toRightOf="@+id/iv_doc_phone"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_weight="1"/>
    android:id="@+id/tv_qq"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="QQ"
        app:layout_constraintLeft_toLeftOf="@+id/iv_doc_qq"
        app:layout_constraintTop_toBottomOf="@+id/iv_doc_qq"
        app:layout_constraintRight_toRightOf="@+id/iv_doc_qq"/>
    android:id="@+id/tv_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="手机"
        app:layout_constraintTop_toBottomOf="@+id/iv_doc_phone"
        app:layout_constraintLeft_toLeftOf="@+id/iv_doc_phone"
        app:layout_constraintRight_toRightOf="@+id/iv_doc_phone"/>
    android:id="@+id/tv_wx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="微信"
        app:layout_constraintTop_toBottomOf="@+id/iv_doc_wx"
        app:layout_constraintLeft_toLeftOf="@+id/iv_doc_wx"
        app:layout_constraintRight_toRightOf="@+id/iv_doc_wx"/>
    android:layout_width="match_parent"
        android:layout_height="10dp"
        app:layout_constraintTop_toBottomOf="@+id/tv_wx"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

3.2 dialog_back.xml //对话框背景

"1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    
    android:color="#f3f3f3"/>
    
    android:topLeftRadius="8dp"
        android:topRightRadius="8dp"/>

UI结构图:

Android 好看的底部弹出UI对话框_第2张图片

3.3 styles.xml中新建style标签选项




3.4 ProgramSelectDialog继承Dialog

	内部定义OnClickListener接口给使用者
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        //创建应用选择对话
        final ProgramSelectDialog dialog = new ProgramSelectDialog(this,
                new ProgramSelectDialog.OnClickListener() {
            @Override
            public void onClick(Dialog dialog, int rate) {
                if(rate == 0) {
                    Toast.makeText(MainActivity.this,"QQ",Toast.LENGTH_SHORT).show();
                }else if(rate == 1){
                    Toast.makeText(MainActivity.this,"手机",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this,"微信",Toast.LENGTH_SHORT).show();
                }
                dialog.dismiss();
            }
        });

        Button button = findViewById(R.id.button);
        //为Button控件设置监听
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show();
            }
        });
    }
}

3.5 MainActivity.class中使用

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        //创建应用选择对话
        final ProgramSelectDialog dialog = new ProgramSelectDialog(this,
                new ProgramSelectDialog.OnClickListener() {
            @Override
            public void onClick(Dialog dialog, int rate) {
                if(rate == 0) {
                    Toast.makeText(MainActivity.this,"QQ",Toast.LENGTH_SHORT).show();
                }else if(rate == 1){
                    Toast.makeText(MainActivity.this,"手机",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this,"微信",Toast.LENGTH_SHORT).show();
                }
                dialog.dismiss();
            }
        });

        Button button = findViewById(R.id.button);
        //为Button控件设置监听
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show();
            }
        });
    }
}

Android 好看的底部弹出UI对话框_第3张图片
下载地址:链接

你可能感兴趣的:(Android)