日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 自定义AlertDialog,调用方法与系统一致

發布時間:2025/3/20 Android 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 自定义AlertDialog,调用方法与系统一致 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

由于android原生的AlertDialog都一致,有時為了和你的項目的Dialog保持一致,你最先想到的就是有沒有AlertDialog相關的style,但據我的查找,官方沒有提供明確的文檔來修改其樣式,所以我們想到的是自己自定一個AlertDialog

如圖,當然風格可以自己修改

所先把你想要的布局custom_dialog_view

<?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="match_parent"
? ? android:orientation="vertical"
? ? android:gravity="center" >


? ? <!-- 頂部橢園邊緣 -->


? ? ?<ImageView
? ? ? ? android:layout_width="300dp"
? ? ? ? android:layout_height="22dp"
? ? ? ? android:src="@drawable/app_listbk_d" >
? ? </ImageView>
? ? <!-- 中間白色背景,兩個TextView,標題和內容,留一個LinearLayout,在代碼中根據調用動態加上按鈕 -->


? ? <LinearLayout
? ? ? ? android:layout_width="300dp"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:background="@drawable/app_listbk_d"
? ? ? ? android:orientation="vertical" >


? ? ? ? <TextView
? ? ? ? ? ? android:id="@+id/title"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? android:textColor="#000000"
? ? ? ? ? ? android:textSize="35dp" />


? ? ? ? <TextView
? ? ? ? ? ? android:id="@+id/message"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_marginBottom="10dp"
? ? ? ? ? ? android:layout_marginLeft="20dp"
? ? ? ? ? ? android:layout_marginRight="10dp"
? ? ? ? ? ? android:layout_marginTop="20dp"
? ? ? ? ? ? android:textColor="#000000"
? ? ? ? ? ? android:textSize="25dp" />
? ? ? ? <!-- 在LinearLayout中加按鈕 -->


? ? ? ? <LinearLayout
? ? ? ? ? ? android:id="@+id/buttonLayout"
? ? ? ? ? ? android:layout_width="fill_parent"
? ? ? ? ? ? android:layout_height="fill_parent"
? ? ? ? ? ? android:layout_gravity="center"
? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? android:orientation="horizontal" >
? ? ? ? </LinearLayout>
? ? </LinearLayout>
? ? <!-- 底部橢園邊緣 -->


? ? ?<ImageView
? ? ? ? android:layout_width="300dp"
? ? ? ? android:layout_height="22dp"
? ? ? ? android:layout_marginTop="-2dp"
? ? ? ? android:src="@drawable/app_listbk_d" >
? ? </ImageView>


</LinearLayout>


然后借助原生的AlertDialog來重寫一下這個控制來達到我們的需求



package com.example.testapi.widget;


import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;


import com.example.testapi.R;


public class AlertDialog {
? ? Context context;
? ? android.app.AlertDialog ad;
? ? TextView titleView;
? ? TextView messageView;
? ? LinearLayout buttonLayout;


? ? public AlertDialog(Context context) {
? ? ? ? this.context = context;
? ? ? ? ad = new android.app.AlertDialog.Builder(context).create();
? ? ? ? ad.show();
? ? ? ? // Replace the source alert dialog.
? ? ? ? Window window = ad.getWindow();
? ? ? ? window.setContentView(R.layout.custom_dialog_view);
? ? ? ? titleView = (TextView) window.findViewById(R.id.title);
? ? ? ? messageView = (TextView) window.findViewById(R.id.message);
? ? ? ? buttonLayout = (LinearLayout) window.findViewById(R.id.buttonLayout);
? ? }


? ? public void setTitle(int resId)
? ? {
? ? ? ? titleView.setText(resId);
? ? }


? ? public void setTitle(String title) {
? ? ? ? titleView.setText(title);
? ? }


? ? public void setMessage(int resId) {
? ? ? ? messageView.setText(resId);
? ? }


? ? public void setMessage(String message)
? ? {
? ? ? ? messageView.setText(message);
? ? }


? ? /**
? ? ?* Button style
? ? ?*?
? ? ?* @param text
? ? ?* @param listener
? ? ?*/
? ? public void setPositiveButton(String text, final View.OnClickListener listener)
? ? {
? ? ? ? Button button = new Button(context);
? ? ? ? LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
? ? ? ? ? ? ? ? LayoutParams.WRAP_CONTENT);
? ? ? ? button.setLayoutParams(params);
? ? ? ? button.setBackgroundResource(R.drawable.app_listbk);
? ? ? ? button.setText(text);
? ? ? ? button.setTextColor(Color.WHITE);
? ? ? ? button.setTextSize(20);
? ? ? ? button.setOnClickListener(listener);
? ? ? ? buttonLayout.addView(button);
? ? }


? ? /**
? ? ?* ?Button style
? ? ?*?
? ? ?* @param text
? ? ?* @param listener
? ? ?*/
? ? public void setNegativeButton(String text, final View.OnClickListener listener)
? ? {
? ? ? ? Button button = new Button(context);
? ? ? ? LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
? ? ? ? ? ? ? ? LayoutParams.WRAP_CONTENT);
? ? ? ? button.setLayoutParams(params);
? ? ? ? button.setBackgroundResource(R.drawable.app_listbk);
? ? ? ? button.setText(text);
? ? ? ? button.setTextColor(Color.WHITE);
? ? ? ? button.setTextSize(20);
? ? ? ? button.setOnClickListener(listener);
? ? ? ? if (buttonLayout.getChildCount() > 0)
? ? ? ? {
? ? ? ? ? ? params.setMargins(20, 0, 0, 0);
? ? ? ? ? ? button.setLayoutParams(params);
? ? ? ? ? ? buttonLayout.addView(button, 1);
? ? ? ? } else {
? ? ? ? ? ? button.setLayoutParams(params);
? ? ? ? ? ? buttonLayout.addView(button);
? ? ? ? }


? ? }


? ? /**
? ? ?* dismiss dialog
? ? ?*/
? ? public void dismiss() {
? ? ? ? ad.dismiss();
? ? }


}

這樣我們在自己的項目中就可以像使用原生的AlertDialog一樣正常使用了

final AlertDialog ad = new AlertDialog(DialogStyleActivity.this);
? ? ad.setTitle("標題");
? ? ad.setMessage("dkdkkdkdk順水有晨在于 在在在在在礙御用有有有地區專業分工有地介是的和無本之木工;地有膽有關有關 " +
? ? "耨溪水源源源碼在緊俏商品22是否 2進行大躍進要核工業部");
? ? ad.setPositiveButton("確定", new OnClickListener() {


? ? ? ? @Override
? ? ? ? public void onClick(View v) {
? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? ad.dismiss();
? ? ? ? ? ? Toast.makeText(DialogStyleActivity.this, "被點到確定", Toast.LENGTH_LONG).show();


? ? ? ? }
? ? });


? ? ad.setNegativeButton("取消", new OnClickListener() {


? ? ? ? @Override
? ? ? ? public void onClick(View v) {
? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? ad.dismiss();
? ? ? ? ? ? Toast.makeText(DialogStyleActivity.this, "被點到取消", Toast.LENGTH_LONG).show();
? ? ? ? }
? ? });
? ? }


轉載于:https://my.oschina.net/u/1244156/blog/205750

總結

以上是生活随笔為你收集整理的Android 自定义AlertDialog,调用方法与系统一致的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲欧美日韩专区 | 国产精品黄网站 | 国产999精品久久久久久 | 久久久网站| 成人动漫av | 4438x在线观看 | 国产一级淫片a视频免费观看 | 日韩成人一区二区三区 | 人人舔人人爽 | 日本一区二区视频在线观看 | 国产福利不卡视频 | 成人免费a级片 | 日日射av | 奇米婷婷 | 国产精品福利视频 | www.三级 | 天天操天天爱天天干 | 日本国产一级片 | 精品国产乱码久久久久久久 | 老太婆av | 永久免费未满 | 国产人人看 | 色六月婷婷| 国产精品18p | 一级片免费 | 国产人妻久久精品一区二区三区 | 天天干天天操天天拍 | 插插插综合 | 少妇av一区二区三区无码 | 交视频在线播放 | 国产区91 | 中文字幕日韩精品在线 | 98在线视频| 高清久久| 国产99在线播放 | 国产成人精品av在线观 | 久久午夜网 | 久久精品香蕉 | 亚洲精品视频在线 | 国产素人在线观看 | 熊出没之冬日乐翻天免费高清观看 | 中文字幕乱码在线 | 激情文学久久 | 日韩精品电影一区二区 | 水牛影视av一区二区免费 | 思思在线视频 | 粗大挺进潘金莲身体在线播放 | 国语对白91| 蜜臀视频在线播放 | 色哟哟欧美精品 | 午夜寂寞少妇 | 国产aⅴ一区二区三区 | 日韩精品在线第一页 | 亚洲一区二区播放 | 午夜青青草 | 日本欧美在线 | 91成品人影院 | 蜜桃av在线 | 91美女免费看 | 污污的视频在线观看 | av免费福利 | 黑人超碰 | 91极品蜜桃臀 | 欧美日韩在线视频免费 | 日韩专区在线观看 | 少妇高潮一区二区三区在线 | 蜜臀aⅴ免费一区二区 | 国产精品国语对白 | 91最新入口 | 亚洲精品乱码久久久久久黑人 | 欧美一区二区三区啪啪 | 秋霞影院午夜丰满少妇在线视频 | 欧美大奶在线 | 在线一区不卡 | 国产在线拍揄自揄拍无码视频 | 91大神在线观看视频 | 毛片久久久 | 国产精品视频网站 | 日韩经典在线 | 精品人妻一区二区免费 | 啪视频免费 | 91免费版黄| 日本中文在线视频 | 香蕉成人网| 美国三级a三级18 | 欧美乱淫 | 国产黄在线观看 | 免费在线观看a视频 | 91久久久久久久久久久久久 | 日本大尺度吃奶做爰视频 | 97人妻精品视频一区 | 一区二区三区91 | 国内精品偷拍视频 | 日韩乱码人妻无码系列中文字幕 | 椎名由奈av一区二区三区 | 国产精品一区二区在线播放 | 国产精品免费无遮挡无码永久视频 | 色呦呦免费 | 天天操天天插 |