Android之自定义dialog

最近做开发需要用到一个自定义的dialog,在网上浏览了很多这方面的问题,基本都写的比较复杂难懂(对菜鸟来说),需要extends Dialog或者修改style之类的,最后自己写了一个dialog(不算是真正意义上的dialog),很好的解决了我所遇到的问题。下面,将我的做法分享给大家。

我是使用Android Studio 1.4,基于android5.1写的一个demo。

MainActivity布局文件activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_centerInParent="true"
        android:id="@+id/btn"
        android:layout_width="200dp"
        android:layout_height="80dp"
        android:background="@drawable/bg_btn"
        android:text="点击显示自定义dialog"
        android:textColor="#ffffffff"
        android:textSize="23sp"/>

</RelativeLayout>

自定义dialog的布局文件layout_customdialog.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:layout_marginTop="200dp"
    >

    <RelativeLayout
        android:id="@+id/rl_dialog_content"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:background="@drawable/bg_dialog">

        <View
            android:paddingTop="20dp"
            android:layout_marginTop="30dp"
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:background="#1586f6"/>

        <TextView
            android:id="@+id/dialog_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:maxLines="5"
            android:gravity="center"
            />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:layout_margin="30dp">

            <Button
                android:id="@+id/dialog_cancel"
                android:text="取消"
                android:background="@drawable/bg_btn"
                android:layout_height="30dp"
                android:layout_width="5dp"
                android:layout_weight="1"
                android:textColor="#FFFFFFFF"
                />

            <Button
                android:id="@+id/dialog_ok"
                android:text="确定"
                android:textColor="#FFFFFFFF"
                android:layout_height="30dp"
                android:layout_width="5dp"
                android:background="@drawable/bg_btn"
                android:layout_weight="1"
                android:layout_marginLeft="40dp"/>

        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>

主界面Java代码MainActivity.java :

package com.weizhi.mycustomdialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    public Context context ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this ;

        Button button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               btnDoSomething();

            }
        });
    }

    public void btnDoSomething(){
        LayoutInflater inflater = LayoutInflater.from(context);
        RelativeLayout relativeLayout = (RelativeLayout)inflater.inflate(R.layout.layout_customdialog, null );

        //对话框
        final Dialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        dialog.getWindow().setContentView(relativeLayout);

        TextView dialogText = (TextView) relativeLayout.findViewById(R.id.dialog_text);
        dialogText.setText("是否返回主界面?");

        //取消按钮
        Button btnCancel = (Button) relativeLayout.findViewById(R.id.dialog_cancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //添加点击事件
                dialog.dismiss();
            }
        });

        //确定按钮
        Button btnOK = (Button) relativeLayout.findViewById(R.id.dialog_ok);
        btnOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //添加点击事件
                dialog.dismiss();
            }
        });

    }
}

效果图如下所示:
主界面
自定义dialog样式
就这样,很简单的。谢谢阅读。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android自定义dialog是一种可以根据开发者的需求进行自定义的对话框。通过自定义dialog,开发者可以实现特定的界面设计和功能。根据引用,我们可以使用AlertDialog.Builder来创建一个普通的弹出框。而根据引用,我们可以通过简单的设置和技巧来实现Android自定义dialog。如果想要对话框具有自定义布局,可以创建一个布局文件,然后通过调用AlertDialog.Builder的setView()方法将其添加到对话框中。默认情况下,自定义布局会填充对话框窗口,但是仍然可以使用AlertDialog.Builder的其他方法来添加按钮和标题。此外,开发者还可以完全自定义一个Dialog,需要进行以下几步: 1. 定义一个style 2. 创建一个继承自Dialog的子类 3. 在子类中实现自定义布局和相关功能 4. 在应用中调用自定义dialog的实例来显示对话框。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [android自定义Dialog](https://blog.csdn.net/weixin_41796401/article/details/96636408)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Android自定义dialog简单实现方法](https://download.csdn.net/download/weixin_38736562/12800768)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值