Android---自定义Dialog、Toast

本文详细讲解了如何在Android中自定义Dialog和Toast,包括创建dialog_layout.xml布局,activity_main.xml,创建CustomDialog类,MainActivity的实现,以及自定义Dialog样式,并附带运行截图和源码下载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇文章简单介绍了怎样利用布局文件自定义Dialog

1.首先自定义一个布局文件dialog_layout.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="wrap_content"
    android:orientation="vertical" >

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注销账号"
        android:gravity="center"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        android:textColor="#ff646464"
        android:textSize="16sp"
        android:textStyle="bold"
        />

    <View 
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@drawable/pressed_bg"
        />

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button 
            android:id="@+id/btn_logout"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="@drawable/row_selector"
            android:text="退出"
            android:paddingTop="15dp"
            android:paddingBottom="15dp"
            />
        <View 
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@drawable/pressed_bg"
            />
        <Button 
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="@drawable/row_selector"
            android:text="取消"
            android:paddingTop="15dp"
            android:paddingBottom="15dp"
            />
    </LinearLayout>
</LinearLayout>

2.主界面布局文件activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.customdialog.MainActivity" >

    <Button 
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show Dialog"
        />
    <Button 
        android:id="@+id/btn2"
        android:layout_below="@id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show Toast"
        />

</RelativeLayout>

3.新建CustomDialog继承自Dialog

public class CustomDialog extends Dialog implements OnClickListener{

    private Activity mActivity;
    private Button btn_logout, btn_cancel;

    public CustomDialog(Activity activity, int theme) {
        super(activity, theme);
        this.mActivity = activity;
    }

    public CustomDialog(Activity activity) {
        this(activity, R.style.dialog_style);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_logout);
        initView();
    }

    private void initView() {

        btn_logout = (Button) findViewById(R.id.btn_logout);
        btn_logout.setOnClickListener(this);

        btn_cancel = (Button) findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
        case R.id.btn_logout:
            Toast.makeText(mActivity, "退出", Toast.LENGTH_SHORT).show();
            break;
        case R.id.btn_cancel:
            CustomDialog.this.dismiss();
            Toast.makeText(mActivity, "取消", Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
        }
    }
}

4.MainActivity文件

package com.example.customdialog;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener {

    private Button btn, btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        btn2 = (Button) findViewById(R.id.btn2);
        btn.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn:
            showCustomDialog();
            break;
        case R.id.btn2:
            showCustomToast();
            break;
        default:
            break;
        }
    }

    private void showCustomDialog() {
        CustomDialog dialog = new CustomDialog(this);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }

    private void showCustomToast() {
        View layout = getLayoutInflater().inflate(R.layout.toast_layout,
                (ViewGroup) findViewById(R.id.toast_layout_root));
        TextView tv = (TextView) layout.findViewById(R.id.text);
        tv.setText("This is custom toast");
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }
}

5.自定义Dialog的样式

<style name="dialog_style" parent="@android:style/Theme.Translucent">
        <!-- 背景颜色及透明程度 -->
        <item name="android:windowBackground">@android:color/white</item>
        <!-- 是否有标题 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 是否浮现在activity之上 -->
        <item name="android:windowIsFloating">true</item>
        <!--半透明-->
        <item name="android:windowIsTranslucent">true</item>
        <!-- 是否模糊 -->
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowFrame">@null</item>
</style>

运行截图:
这里写图片描述
这里写图片描述

源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值