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,调用方法与系统一致相关推荐
- android 自定义输入法布局,Android自定义输入法使用方法
Android自定义输入法使用方法 时间:2017-04-21 来源:Android开发学习网 对于Android用户而言,一般都会使用第三方的输入法.可是在输入密码时(尤其是支付相关的密码) ...
- Android自定义AlertDialog的控件获取操作
Android自定义AlertDialog的控件获取操作 在自定义的AlertDialog布局虽然可以显示,但是试过很多方法都不能获得其中的控件进行操作,找了很多方法最后这种方法可以. dialog的 ...
- Python自定义类调用方法
自定义类调用方法:文件名字小写,类名字大写,从文件名导入类名就行,保证文件名字和要调用的文件在一个文件夹. 类保存在类文件里面,在新文件里面调用类 比如 from alien import Alien ...
- Android 自定义AlertDialog对话框
2019独角兽企业重金招聘Python工程师标准>>> 系统默认的AlertDialog,与项目的UI不统一,所以,改了一下,定义了一样式,最终效果如下图: 另外,为了尽量少改原来的 ...
- Android 自定义AlertDialog
Android 自定义popuWindow 对于项目需求中,系统的弹出框不能满足我们的需求,就只能通过自定义布局来实现自己相对应的功能,这里总结一篇关于AlertDialog的 具体操作 这里把弹出框 ...
- android activity传值到dialog,android 自定义AlertDialog 与Activity相互传递数据
**主要实现功能:** 1.从Activity的TextView中获取字符串设置到AlertDialog的TextView和EditText中 2.将AlertDialog的EditText中的值设置 ...
- Android 自定义AlertDialog类
还是先从最简单的开始吧,然后一步一步的扩展. 为了保证软件上所谓的低耦合度和可重用性,这里不得不需要单独建立一个类CustomerDialog,然后继承AlertDialog public class ...
- Android 自定义view onMeasure() 方法剖析
接着上一篇自定义view 相关的,揭秘 Android Graphics2D 实现动态效果之--invalidate() 内容的介绍,这一篇主要介绍下自定义view 中的 onMeasure()方 ...
- android js接口调用方法,详解Android JS相互调用
最近在研究Android.JS相互调用,之前没怎么接触过,只知道loadUrl()就可以加载一个网页了,研究过之后发现Android可以调JS,JS也可以调Android原生控件,很开心啊.下面小编就 ...
最新文章
- Markdown基本语法使用
- python发明者叫什么-Guido发明的python语言是哪一年正式发布的?
- laravel php配置,PHP Laravel框架路由配置及设置技巧全解
- vue require css html,requirejs vue vue.router简单框架
- 手把手教你用Java实现一个简易联网坦克对战小游戏
- 神奇的仙丹,性感的Elixir
- Bailian3248 最大公约数【数论】
- 国科大学习资料--模式识别与机器学习(黄庆明)--期末复习题2(含答案)
- javascript中的二维数组定义
- 一起看 I/O | 全新 Google Play SDK 索引助您为应用选择合适的 SDK
- SIPC的认证算法java实现
- docker安装dejavu
- 【Win10 C盘压缩卷问题解答】:无法将卷压缩到超出任何不可移动的文件所在点
- OpenCV - Universal intrinsics 统一指令集
- 进入BeOS的花花世界 系列六
- Arcgis中把背景值为0的图像背景改为nodate
- append()函数
- IDM Trial Reset下载|InternetDownloadManager无限试用工具下载
- js逆向-ast混淆还原入门案例(2)
- /*1. 查询SC表中的全部数据。*/ SELECT * FROM SC /*2. 查询计算机系学生的姓名和年龄。*/ SELECT Sname,Sex FROM Student WHERE Sd
热门文章
- 如何绘制漂亮的多序列比对图片
- 阿里云Ubuntu安装图形界面与中文语言包
- java操作word (jacob)
- [转载]Apache之Hadoop学习:初识hadoop
- 安装卸载功能 [测试思路]
- 多表联合查询存储过程
- 【干货】周鸿祎谈雷军:能不能All In是一个核武器
- php解析html类库simple_html_dom
- 2519485c7dcfe0295a41d4e0f69bb10a 求解
- redis编译安装:make 的新错误--collect2: ld returned 1 exit status