自定义的底部弹出Dialog,设置为抽象类,在使用的Activity中实现具体的点击内容,使用style修改dialog的部分显示效果,设置Window属性使dialog位于屏幕底部。
java代码:
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
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 com.xxx.R;
public abstract class DialogGetHeadPicture extends Dialog implements View.OnClickListener{
private Activity activity;
private FrameLayout flt_amble_upload, flt_take_photo_upload;
private Button btn_cancel;
public DialogGetHeadPicture(Activity activity) {
super(activity, R.style.MyDialogTheme);
this.activity = activity;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_setting_get_head_picture);
flt_amble_upload = (FrameLayout) findViewById(R.id.flt_amble_upload);
flt_take_photo_upload = (FrameLayout) findViewById(R.id.flt_take_photo_upload);
btn_cancel = (Button) findViewById(R.id.btn_cancel);
flt_amble_upload.setOnClickListener(this);
flt_take_photo_upload.setOnClickListener(this);
btn_cancel.setOnClickListener(this);
setViewLocation();
setCanceledOnTouchOutside(true);//外部点击取消
}
/**
* 设置dialog位于屏幕底部
*/
private void setViewLocation(){
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels;
Window window = this.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.x = 0;
lp.y = height;
lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
// 设置显示位置
onWindowAttributesChanged(lp);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.flt_amble_upload:
amble();
this.cancel();
break;
case R.id.flt_take_photo_upload:
photo();
this.cancel();
break;
case R.id.btn_cancel:
this.cancel();
break;
}
}
public abstract void amble();
public abstract void photo();
}
界面xml::
<?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:background="#d3d3d3"
android:orientation="vertical">
<FrameLayout
android:id="@+id/flt_amble_upload"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="1px"
android:background="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="@mipmap/ic_setting_amble"
android:drawablePadding="13dp"
android:gravity="center"
android:text="@string/amble_upload"
android:textColor="#000000"
android:textSize="15sp" />
</FrameLayout>
<FrameLayout
android:id="@+id/flt_take_photo_upload"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="2px"
android:background="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="@mipmap/ic_setting_photo"
android:drawablePadding="13dp"
android:gravity="center"
android:text="@string/take_photo_upload"
android:textColor="#000000"
android:textSize="15sp" />
</FrameLayout>
<Button
android:id="@+id/btn_cancel"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="2px"
android:background="#ffffff"
android:gravity="center"
android:text="@string/cancel"
android:textColor="#000000"
android:textSize="15sp" />
</LinearLayout>
文字(strings.xml):
<string name="amble_upload">相册上传</string>
<string name="take_photo_upload">拍照上传</string>
<string name="cancel">取消</string>
主题(styles.xml):
<style name="MyDialogTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:windowFrame">@null</item><!-- 边框 -->
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item><!-- 外部变灰 -->
<item name="android:windowContentOverlay">@null</item><!-- 内部阴影 -->
<item name="android:windowAnimationStyle">@style/dialog_animation</item>
</style>
<style name="dialog_animation" parent="@android:style/Animation.Dialog">
<item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
<item name="android:windowExitAnimation">@anim/push_bottom_out</item>
</style>
动画(anim文件夹)
push_bottom_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="300"
android:fromYDelta="100%p"
android:toYDelta="0" />
</set>
push_bottom_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="300"
android:fromYDelta="0"
android:toYDelta="50%p" />
</set>
使用例子:
new DialogGetHeadPicture(Setting.this){
@Override
public void amble() {
//TODO 从相册获取照片
}
@Override
public void photo() {
//TODO 拍照获取照片
}
}.show();
完成效果:
参考文章:
Android自定义底部弹出Dialog