Android自定義彈出框dialog效果
項(xiàng)目要用到彈出框,還要和蘋果的樣式一樣(Android真是沒地位),所以就自己定義了一個(gè),不是很像(主要是沒圖),但是也還可以。
廢話不多說了,直接上代碼
1、先看布局文件
<?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:padding="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="@drawable/custom_dialog_background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title_custom_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="提醒"
android:textColor="#000"
android:textSize="18dp" />
<TextView
android:id="@+id/tv_message_custom_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="您確定要取消訂單嗎" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="20dp"
android:background="#dfdfdf" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_negative_custom_dialog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:text="取消"
android:textColor="@android:color/holo_blue_dark" />
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#dfdfdf" />
<Button
android:id="@+id/btn_positive_custom_dialog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:text="確定"
android:textColor="@android:color/holo_blue_dark" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
2、集成dialog重寫了一下
package newair.com.storelibrary.ui.custom.widget;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import newair.com.storelibrary.R;
/**
* Created by ouhimehime on 16/4/22.
* ---------自定義提示框-----------
*/
public class CustomDialog extends Dialog {
public CustomDialog(Context context) {
super(context);
}
public CustomDialog(Context context, int themeResId) {
super(context, themeResId);
}
protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
public static class Builder {
private Context context;
private String title; //標(biāo)題
private String message;//提示消息
private String negative_text;//消極的
private String positive_text;//積極的
private DialogInterface.OnClickListener negativeListener;//消極的監(jiān)聽
private DialogInterface.OnClickListener positiveListener;//積極的監(jiān)聽
public Builder(Context context) {
this.context = context;
}
public Builder setTitle(String title) {
if (title == null) {
this.title = "提醒";
}
this.title = title;
return this;
}
public Builder setMessage(String message) {
if (message == null) {
this.message = "您沒有填寫提示信息哦";
}
this.message = message;
return this;
}
public Builder setNegativeButton(String negative_text, DialogInterface.OnClickListener negativeListener) {
if (negative_text == null) {
this.negative_text = "取消";
}
this.negative_text = negative_text;
this.negativeListener = negativeListener;
return this;
}
public Builder setPositionButton(String positive_text, DialogInterface.OnClickListener positiveListener) {
if (positive_text == null) {
this.positive_text = "確定";
}
this.positive_text = positive_text;
this.positiveListener = positiveListener;
return this;
}
private TextView tv_title_custom_dialog; //標(biāo)題
private TextView tv_message_custom_dialog;//提示信息
private Button btn_negative_custom_dialog;//消極
private Button btn_positive_custom_dialog;//積極
public CustomDialog create() {
final CustomDialog dialog = new CustomDialog(context);
View view = LayoutInflater.from(context).inflate(R.layout.dialog_custom_style_layout, null);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上這一句,取消原來的標(biāo)題欄,沒加這句之前,發(fā)現(xiàn)在三星的手機(jī)上會(huì)有一條藍(lán)色的線
// dialog.addContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
tv_title_custom_dialog = (TextView) view.findViewById(R.id.tv_title_custom_dialog);
tv_message_custom_dialog = (TextView) view.findViewById(R.id.tv_message_custom_dialog);
btn_negative_custom_dialog = (Button) view.findViewById(R.id.btn_negative_custom_dialog);
btn_positive_custom_dialog = (Button) view.findViewById(R.id.btn_positive_custom_dialog);
tv_title_custom_dialog.setText(title);
tv_message_custom_dialog.setText(message);
btn_negative_custom_dialog.setText(negative_text);
btn_positive_custom_dialog.setText(positive_text);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
btn_negative_custom_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
negativeListener.onClick(dialog, Dialog.BUTTON_NEGATIVE);
}
});
btn_positive_custom_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
positiveListener.onClick(dialog, Dialog.BUTTON_POSITIVE);
}
});
return dialog;
}
}
}
3、使用起來和系統(tǒng)的用法一樣
CustomDialog.Builder builder = new CustomDialog.Builder(this);
builder.setTitle("購(gòu)物提醒")
.setMessage("我是提示信息,大家好好")
.setNegativeButton("再看看", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(GoodsListActivity.this, "點(diǎn)擊了取消按鈕", Toast.LENGTH_SHORT).show();
}
})
.setPositionButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(GoodsListActivity.this, "點(diǎn)擊了確定按鈕", Toast.LENGTH_SHORT).show();
}
})
.create()
.show();
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義底部彈出框ButtomDialog
- Android仿微信網(wǎng)絡(luò)加載彈出框
- Android自定義view仿iOS彈出框效果
- Android使用popUpWindow帶遮罩層的彈出框
- Android實(shí)現(xiàn)底部半透明彈出框PopUpWindow效果
- Android實(shí)現(xiàn)蒙版彈出框效果
- Android 多種簡(jiǎn)單的彈出框樣式設(shè)置代碼
- Android實(shí)現(xiàn)可輸入數(shù)據(jù)的彈出框
- Android使用Dialog風(fēng)格彈出框的Activity
- android自定義彈出框樣式的實(shí)現(xiàn)方法
相關(guān)文章
Android實(shí)現(xiàn)ViewPager無限循環(huán)效果(二)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)ViewPager無限循環(huán)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android使用ContentProvider初始化SDK庫方案小結(jié)
這篇文章主要介紹了Android使用ContentProvider初始化SDK庫方案總結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Android實(shí)現(xiàn)將已發(fā)送的短信寫入短信數(shù)據(jù)庫的方法
這篇文章主要介紹了Android實(shí)現(xiàn)將已發(fā)送的短信寫入短信數(shù)據(jù)庫的方法,是Android手機(jī)開發(fā)常見的技巧,需要的朋友可以參考下2014-09-09
實(shí)例講解Android中的AutoCompleteTextView自動(dòng)補(bǔ)全組件
AutoCompleteTextView組件被用在輸入框中能實(shí)現(xiàn)輸入內(nèi)容自動(dòng)補(bǔ)全的功能,類似于大家平時(shí)用Google時(shí)的輸入聯(lián)想,這里我們來用實(shí)例講解Android中的AutoCompleteTextView自動(dòng)補(bǔ)全組件,特別是實(shí)現(xiàn)郵箱地址補(bǔ)全的例子,非常實(shí)用2016-05-05
Android基于ViewPager實(shí)現(xiàn)類似微信頁面切換效果
這篇文章主要介紹了Android基于ViewPager實(shí)現(xiàn)類似微信頁面切換效果,通過Fragment適配器實(shí)現(xiàn)頁面切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android定時(shí)器和倒計(jì)時(shí)實(shí)現(xiàn)淘寶秒殺功能
這篇文章主要為大家詳細(xì)介紹了Android定時(shí)器和倒計(jì)時(shí)實(shí)現(xiàn)淘寶秒殺功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
三種Android單擊事件onclick的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了三種Android單擊事件onclick的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-05-05
SurfaceView開發(fā)[捉小豬]手機(jī)游戲 (二)
這篇文章主要介紹了用SurfaceView開發(fā)[捉小豬]手機(jī)游戲 (二)本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Android編程實(shí)現(xiàn)播放音頻的方法示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)播放音頻的方法,結(jié)合實(shí)例形式分析了Android使用MediaPlayer類播放音頻的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08

