简单好用的自定义Dialog(二)
来源:互联网 发布:手机能用淘宝助理吗 编辑:程序博客网 时间:2024/05/20 10:56
一、简单介绍
安卓提供的dialog的ui实在是丑,实际开发中我们通常自定义view实现我们需要的功能
先来张效果图;
二、具体实现
1)首先边框要是圆角的,不然太丑啦
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#ffffff" /> <corners android:radius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp" /></shape>
android:radius=”5dp” 有了其实下面都不需要了。
2)布局文件
<?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="fill_parent" > <RelativeLayout android:id="@+id/re_tip_dialog" android:layout_width="250dp" android:layout_height="160dp" android:layout_centerInParent="true" android:background="@drawable/rounded_search_text"> <ImageView android:layout_marginTop="10dp" android:layout_centerHorizontal="true" android:src="@drawable/laba" android:id="@+id/appTitle" android:layout_width="70dp" android:layout_height="70dp" /> <LinearLayout android:layout_centerHorizontal="true" android:layout_below="@id/appTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tips1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我要举报" android:layout_marginTop="10dp" android:textColor="#ff0dadf8" /> <TextView android:id="@+id/tips2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textColor="#e70808" /> <TextView android:id="@+id/tips3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="楼网友违规言论" android:layout_marginTop="10dp" android:textColor="#ff0dadf8" /> </LinearLayout> <LinearLayout android:id="@+id/tips" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_weight="1" android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp" android:text="我不举报了" android:textColor="#bcbcbc" android:textAlignment="center" android:background="@drawable/button_jubao" /> <Button android:layout_weight="1" android:id="@+id/sure" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确认举报" android:layout_below="@id/tips" android:textAlignment="center" android:textSize="12sp" android:background="@drawable/button_jubao" android:textColor="#494949" /> </LinearLayout> </RelativeLayout></RelativeLayout>
3)具体的java类,在这里实现
package com.kkeji.news.client.view.Dialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.kkeji.news.client.R;
public class DialogInformAgainst extends Dialog implements
View.OnClickListener {
private onBtnClickListener onBtnClickListener;
private Context context;
private String sure;
private String cancle;
private String appTitle;
private String tips2;
private Button tv_dele_sure;
private Button tv_dele_cancle;
private TextView tv_tips1;
private TextView tv_tips2;
private TextView tv_tips3;
private RelativeLayout re_tip_dialog;
public DialogInformAgainst(Context context, onBtnClickListener onBtnClickListener, String sure, String cancle, String appTitle, String tips2) { super(context); this.onBtnClickListener = onBtnClickListener; this.context = context; this.tips2 = tips2; this.sure = sure; this.cancle = cancle;}@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 去除默认的头部标题 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.dialog_comment_jubao); tv_dele_sure = (Button) findViewById(R.id.sure); tv_dele_cancle = (Button) findViewById(R.id.cancel); tv_tips1 = (TextView) findViewById(R.id.tips1); tv_tips2 = (TextView) findViewById(R.id.tips2); tv_tips3 = (TextView) findViewById(R.id.tips3); re_tip_dialog = (RelativeLayout) findViewById(R.id.re_tip_dialog); tv_dele_cancle.setOnClickListener(this); tv_dele_sure.setOnClickListener(this); //进入动画是伪代码 ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(200); re_tip_dialog.startAnimation(scaleAnimation); Window window = this.getWindow(); WindowManager.LayoutParams params = this.getWindow().getAttributes(); // 去除四角黑色背景 window.setBackgroundDrawable(new BitmapDrawable()); // 设置周围的暗色系数 params.dimAmount = 0.5f; params.y=-280; window.setAttributes(params); // 为各个textview赋值 tv_dele_sure.setText(sure); tv_dele_cancle.setText(cancle); tv_tips2.setText(tips2);}public interface onBtnClickListener { public void onSure(); public void onExit();}@Overridepublic void onClick(View v) { switch (v.getId()) { case R.id.cancel: onBtnClickListener.onExit(); alertDialogExitAnim(); break; case R.id.sure: onBtnClickListener.onSure(); alertDialogExitAnim(); break; default: break; }}private void alertDialogExitAnim() { ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.ABSOLUTE, re_tip_dialog.getWidth() / 2, Animation.ABSOLUTE, re_tip_dialog.getHeight() / 2); scaleAnimation.setDuration(1000); re_tip_dialog.startAnimation(scaleAnimation); scaleAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { DialogInformAgainst.this.dismiss(); } });}
}
4)最后是实现调用,我是跟js交互写的,你们忽略就好
@JavascriptInterface public void postReport(final int pRid,String floor) { new DialogInformAgainst(ActivityArticleContent.this, new DialogInformAgainst.onBtnClickListener() { @Override public void onSure() { mCommentsHelper.postUserReport(mNewsArticle.getArticle_id(), pRid, new CommentsHelper.GetNewsCommentsContent() { //举报成功 @Override public void onSuccess(int pStatusCode, String pRequestString) { if (pStatusCode == 200) { //成功 MLog.i(TAG, "postReport" + pRid + " "); Toast.makeText(ActivityArticleContent.this, "谢谢您的举报,我们会尽快处理", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(int pStatusCode) { //失败 MLog.i(TAG, "postReport" + pRid + " error"); } }); } @Override public void onExit() { Toast.makeText(ActivityArticleContent.this, "取消举报", Toast.LENGTH_SHORT).show(); } }, "确定举报", "不举报了", "提示", floor+"").show(); }
三、知识点梳理。
主要是在自定义java中写接口方法以实现回调。背景颜色,字体等能在自定义java中指定,也可以在调用的时候进行指定。diaolog的大小和显示位置都可以在自定义view中进行设置。
- 简单好用的自定义Dialog(二)
- 简单好用的自定义Dialog(一)
- 好用的dialog
- 自定义Dialog的简单实现
- 简单快捷的自定义 Dialog
- Android 简单的自定义Dialog
- 一个简单的自定义Dialog
- Android 简单的自定义dialog
- 一个简单的自定义Dialog
- Android 自定义dialog(封装好的)
- 自定义控件(二)自定义Dialog
- 简单好用的自定义DialogFragment
- android最简单的自定义全屏Dialog
- Android简单实用的自定义Dialog
- Android自定义最简单的dialog
- android 自定义dialog的简单实现
- Android 自定义Dialog的简单实现
- 实现一个简单的自定义Dialog弹窗。
- easyui学习笔记-datagrid中相关学习
- Java Web 学习笔记(一):开发环境搭建
- minicom 使用
- Linux下的常用操作
- Selenium疑问贴(持续更新中)
- 简单好用的自定义Dialog(二)
- 安卓高效框架简单使用
- HTTP Status Code 说明
- iOS回顾笔记( 01 )
- 电商产品设计:怎样设计实用的商品中心
- MB中消息集校验功能实现例子
- 近几日使用strtok的一点心得体会
- (1)caffe总结之Ubuntu 14.04 + Caffe + Cuda 7.5 + Opencv 3.0安装教程
- java线程池理解