style 样式:
<style name="BottomDialog">
<item name="android:windowNoTitle">true
<item name="android:windowIsFloating">true
<item name="android:windowEnterAnimation">@anim/push_bottom_in
<item name="android:windowExitAnimation">@anim/push_bottom_out
<item name="android:windowContentOverlay">@null
</style>
关键在于Theme.Dialog中的各种属性:
<style name="Theme.Dialog" parent="Theme.AppCompat.Light.Dialog">
<item name="windowActionBar">false</item>
<!-- 没有标题栏 -->
<item name="windowNoTitle">true</item>
<!--边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!-- 是否透明 -->
<item name="android:windowIsTranslucent">true</item>
<!--除去title-->
<item name="android:windowNoTitle">true</item>
<!-- 对话框是否有遮盖 -->
<item name="android:windowContentOverlay">@null</item>
<!-- 对话框出现时背景是否变暗 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 背景颜色,因为windowBackground中的背景已经写死了,所以这里的设置无效 -->
<item name="android:colorBackground">@color/background_floating_material_light</item>
<!-- 着色缓存(一般不用)-->
<item name="android:colorBackgroundCacheHint">@null</item>
<!-- 标题的字体样式 -->
<item name="android:windowTitleStyle">@style/RtlOverlay.DialogWindowTitle.AppCompat</item>
<item name="android:windowTitleBackgroundStyle">@style/Base.DialogWindowTitleBackground.AppCompat</item>
<!--对话框背景(重要) , default = abc_dialog_material_background-->
<item name="android:windowBackground">@drawable/dialog_bg_custom</item>
<!-- 动画 -->
<item name="android:windowAnimationStyle">@style/Animation.AppCompat.Dialog</item>
<!-- 输入法弹出时自适应 -->
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="windowActionModeOverlay">true</item>
<!-- 列表部分的内边距,作用于单选、多选列表 -->
<item name="listPreferredItemPaddingLeft">20dip</item>
<item name="listPreferredItemPaddingRight">24dip</item>
<item name="android:listDivider">@null</item>
<!-- 单选、多选对话框列表文字的颜色 默认:@color/abc_primary_text_material_light -->
<item name="textColorAlertDialogListItem">#00ff00</item>
<!-- 单选、多选对话框的分割线 -->
<!-- dialog中listView的divider 默认:@null-->
<item name="listDividerAlertDialog">@drawable/divider</item>
<!-- 单选对话框的按钮图标 (默认不为null)-->
<item name="android:listChoiceIndicatorSingle">@android:drawable/btn_radio</item>
<!-- 对话框整体的内边距,但不作用于列表部分 默认:@dimen/abc_dialog_padding_material-->
<item name="dialogPreferredPadding">20dp</item>
<item name="alertDialogCenterButtons">true</item>
<!-- 对话框内各个布局的布局文件-->
<item name="alertDialogStyle">@style/AlertDialogStyle</item>
</style>
自定义Dialog类:
package com.hongshi.wuliudidi.czrestruct.dialog;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.aigestudio.wheelpicker.WheelPicker;
import com.hongshi.wuliudidi.R;
import java.util.ArrayList;
import java.util.Calendar;
/**
* @author sunny
* @version ${VERSION}
* @created 2019/6/14
* @title
* @description
* @changeRecord 2019/6/14 modify by sunny
*
* 选择性别的弹框
*/
public class SexWheelDialogextends Dialogimplements View.OnClickListener {
private WheelPickersexPicker;
private TextViewcancelView, titleView, okView;
/**
* 选择的天索引
*/
private int sexSelectIndex =0;
private ArrayListsexList =new ArrayList<>();
public interface SexWheelSelectBack {
void select(String sex);
}
private SexWheelSelectBackcallback;
public SexWheelDialog(Context context) {
super(context, R.style.BottomDialog);
initView();
}
public SexWheelDialog(Context context, int theme) {
super(context, theme);
initView();
}
public void initView() {
setContentView(R.layout.rz_dialog_sex);
sexPicker = findViewById(R.id.dialog_sex_picker);
okView = findViewById(R.id.dialog_date_commit);
cancelView = findViewById(R.id.dialog_date_close);
titleView = findViewById(R.id.dialog_date_title);
initDailogArr();
initData();
initEvent();
}
private void initDailogArr() {
setCanceledOnTouchOutside(false);
Window window = getWindow();
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
window.setGravity(Gravity.BOTTOM);
window.setWindowAnimations(R.style.BottomDialog);//AnimBottom
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.dialog_date_close:
dismiss();
break;
case R.id.dialog_date_commit:
if (this.callback !=null) {
dismiss();
String sex =sexList.get(sexSelectIndex);
callback.select(sex);
}
break;
}
}
private void initEvent() {
sexPicker.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
@Override
public void onItemSelected(WheelPicker picker, Object data, int position) {
sexSelectIndex = position;
}
});
cancelView.setOnClickListener(this);
okView.setOnClickListener(this);
}
public void initData() {
sexList =new ArrayList<>();
sexList.add("男");
sexList.add("女");
sexPicker.setData(sexList);
sexPicker.setSelectedItemPosition(sexSelectIndex);
}
public void showDateDialog(SexWheelSelectBack callback) {
this.callback = callback;
show();
}
public void showDateWheelDiaolog(String title, String sexStr, SexWheelSelectBack callback) {
titleView.setText(title);
this.callback = callback;
for (int i =0; i
if (sexList.get(i).trim() == sexStr) {
sexSelectIndex = i;
return;
}
}
sexPicker.setSelectedItemPosition(sexSelectIndex, true);
show();
}
}
网友评论