做Android應用中,最缺少不了的就是自定義Dialog,對於系統默認提供的Dialog樣式,一般都不復合我們應用的樣式。
自定義Dialog需要3步驟即可:
1、主要的重寫Dialog的Java類
2、自定義佈局文件、並設置Dialog Theme,在style.xml文件中加一個即可
3、使用方法
一、創建CustomPopDialog2.java類
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
/**
* 該自定義Dialog應用在:彈出框居中顯示圖片,點擊其他區域自動關閉Dialog
*
*/
public class CustomPopDialog2 extends Dialog {
public CustomPopDialog2(Context context) {
super(context);
}
public CustomPopDialog2(Context context, int theme) {
super(context, theme);
}
public static class Builder {
private Context context;
private Bitmap image;
public Builder(Context context) {
this.context = context;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public CustomPopDialog2 create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final CustomPopDialog2 dialog = new CustomPopDialog2(context,R.style.Dialog);
View layout = inflater.inflate(R.layout.dialog_share_qrcode, null);
dialog.addContentView(layout, new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT
, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
dialog.setContentView(layout);
ImageView img = (ImageView)layout.findViewById(R.id.img_qrcode);
img.setImageBitmap(getImage());
return dialog;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
這裏簡單說明下,我們自定義Dialog需要準備一個自己的View佈局文件,主要關注create()方法即可,本例中就是直接顯示一個圖片。
二、自定義View的佈局文件、並在style.xml中添加theme
<?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"
android:id="@+id/rootLayout">
<ImageView
android:id="@+id/img_qrcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="二維碼" />
</LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:background">#00000000</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
三、使用自定義的Dialog
Bitmap bitmap = xxxxx;// 這裏是獲取圖片Bitmap,也可以傳入其他參數到Dialog中
CustomPopDialog2.Builder dialogBuild = new CustomPopDialog2.Builder(context);
dialogBuild.setImage(bitmap);
CustomPopDialog2 dialog = dialogBuild.create();
dialog.setCanceledOnTouchOutside(true);// 點擊外部區域關閉
dialog.show();
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
最終效果圖: