https://www.jianshu.com/p/d1852b04a0aa
使用一个DialogFragment一般我们需要重新写一个类来继承DialogFragment,DialogFragment原本其实也是一个fragment,只是其内部绑定了dialog,所以很多操作是差不多的。
创建一个DialogFragment有两个方法,一个是我们比较熟悉的,重新onCreateDialog方法, 返回一个创建的dialog对象就行,具体如下:
public class MyDialogFragment extends DialogFragment {
private static MyDialogFragment_Listener listener1;
private ListView mTvDialogContent;
private TextView mBtnCancel;
private TextView mBtnConfirm;
private String content;//粘贴板内容
private TextView mTvClear;
public static MyDialogFragment newInstance(String content,MyDialogFragment_Listener listener) {
MyDialogFragment customDialogFragment = new MyDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("content", content);
customDialogFragment.setArguments(bundle);
listener1 = listener;
return customDialogFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
content = bundle.getString("content");
}
}
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
if (window != null) {
//设置 window 的背景色为透明色.
//如果通过 window 设置宽高时,想要设置宽为屏宽,就必须调用下面这行代码。
// window.setBackgroundDrawableResource(R.color.transparent);
WindowManager.LayoutParams attributes = window.getAttributes();
//在这里我们可以设置 DialogFragment 弹窗的位置
attributes.gravity = Gravity.CENTER;
//我们可以在这里指定 window的宽高
Display display = getActivity().getWindowManager().getDefaultDisplay();
attributes.width = (int) (display.getWidth()*0.9);
attributes.height = (int) (display.getHeight()*0.4);
//设置 DialogFragment 的进出动画
// attributes.windowAnimations = R.style.DialogAnimation;
window.setAttributes(attributes);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
View rootView = inflater.inflate(R.layout.popup_clipboard, container);
initView(rootView);
return rootView;
}
private void initView(View view) {
mTvDialogContent = view.findViewById(R.id.tv_dialogContent);
mBtnCancel = view.findViewById(R.id.btn_cancel);
mBtnConfirm = view.findViewById(R.id.btn_confirm);
mTvClear = view.findViewById(R.id.clear_tv);
ProCityAreasDao proCityAreasDao = new ProCityAreasDao();
final List<TabProCityAreas> tabProCityAreasList = proCityAreasDao.getdateFromPostcode(content);
PostcodeAdapter postcodeAdapter = new PostcodeAdapter(getContext(),tabProCityAreasList);
postcodeAdapter.setOnItemClickListener(new PostcodeAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
listener1.getDataFrom_DialogFragment(tabProCityAreasList.get(position));
dismiss();
}
});
mTvDialogContent.setAdapter(postcodeAdapter);
mBtnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
mTvClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
if (getActivity() instanceof OnDialogClickListener) {
//传递消息给 Activity
((OnDialogClickListener) getActivity()).clearData(content);
}
}
});
mBtnConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
if (getActivity() instanceof OnDialogClickListener) {
//传递消息给 Activity
((OnDialogClickListener) getActivity()).confirmDialog(content);
}
}
});
}
// 回调接口,用于传递数据给Activity -------
public interface MyDialogFragment_Listener {
void getDataFrom_DialogFragment(TabProCityAreas tabProCityAreas);
}
}
调用
MyDialogFragment dialog = MyDialogFragment.newInstance(data,
new MyDialogFragment.MyDialogFragment_Listener() {
@Override
public void getDataFrom_DialogFragment(TabProCityAreas tabProCityAreas) {
mOrigin.setTextWithoutAnimation(
tabProCityAreas.getTownname() + "," + tabProCityAreas.getAreaname() );
}
}) ;
dialog.show(getSupportFragmentManager(),"dialog");
自定义xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--通过 xml 指定宽高的时候,要嵌套一层布局-->
<!--我们在这里设置宽高为 match_parent 属性的时候,
也必须把 window 的宽高设置为 MATCH_PARENT ,否则无法生效!-->
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorBackgroup"
android:padding="10dp">
<TextView
android:id="@+id/tv_dialogTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorBlack"
android:text="@string/title_select_addr"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/tv_dialogContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:divider="@color/colorLine"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_dialogTitle" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/dp_5"
android:orientation="horizontal"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="@id/tv_dialogContent">
<TextView
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/cancel"/>
<TextView
android:id="@+id/clear_tv"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="清除" />
<TextView
android:id="@+id/btn_confirm"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="粘贴" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</FrameLayout>
在5.0以上的版本中,我们在style中可以查看默认的dialog风格,其实有设置dialog的一些属性,自己定义一种类型的dialog,然后继承就可以修改整体dialog中的风格了,默认动画,背景颜色,或者宽度之类的都是可以的如下:
<style name="MyMinDialogWidth" parent="Base.Theme.AppCompat.Dialog">
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:alertDialogTheme">@style/MyMinDialogWidth</item>
<item name="android:dialogTheme">@style/MyMinDialogWidth</item>
</style>
//Theme.AppCompat.Dialog主题常用的一般有以下属性:
<!-- 背景透明 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 边框 -->
<item name="android:windowFrame">@null</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否半透明 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 是否无标题 -->
<item name="android:windowNoTitle">true</item>
<!-- Dialog背景样式 -->
<item name="android:background">@android:color/transparent</item>
<!-- 模糊 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 遮罩层 -->
<item name="android:backgroundDimAmount">0.5</item>