DialogFragment 实现圆角效果
发布时间:2020-12-16 06:06:59 所属栏目:百科 来源:网络整理
导读:以前使用AlertDialog或者Dialog的时候使用一张xml 圆角的drawable文件作为 dialog的背景即可实现圆角效果。 但是在使用DialogFragment的时候发现 无论如何设置 drawable 文件中 corners属性的radius值,都没有效果。 后来在stackoverflow上面发现了一个解决
以前使用AlertDialog或者Dialog的时候使用一张xml 圆角的drawable文件作为 dialog的背景即可实现圆角效果。 但是在使用DialogFragment的时候发现 无论如何设置 drawable 文件中 corners属性的radius值,都没有效果。 后来在stackoverflow上面发现了一个解决办法。 首先 getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
具体代码如下 这是布局文件 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="200dp" android:layout_height="wrap_content" android:background="@drawable/exit_app_dialog_bg" android:orientation="vertical">
<LinearLayout android:paddingTop="5dp" android:paddingRight="5dp" android:paddingLeft="5dp" android:layout_width="match_parent" android:layout_height="65dp" android:layout_gravity="center" android:background="@drawable/exit_app_dialog_title_bg">
<TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="确定退出应用?" android:textColor="@color/white" />
</LinearLayout>
<View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/gray" />
<LinearLayout android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_marginRight="5dp" android:layout_marginLeft="5dp" android:layout_width="match_parent" android:layout_height="45dp" android:orientation="horizontal">
<TextView android:id="@+id/btn_cancel" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="取消" android:textColor="@color/themeColor" />
<View android:layout_width="1px" android:layout_height="match_parent" android:background="@color/themeColor" />
<TextView android:id="@+id/btn_ok" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#EEEEEE" android:gravity="center" android:text="确定" android:textColor="@color/themeColor" />
</LinearLayout>
</LinearLayout>
这是dialogfragment中内部实现的代码 @Nullable
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
View view = inflater.inflate(R.layout.dialog_exit_application,container,false);
btn_ok = (TextView) view.findViewById(R.id.btn_ok);
btn_cancel = (TextView) view.findViewById(R.id.btn_cancel);
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
positiveListener.onPositiveClick(v);
getDialog().dismiss();
}
});
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
negativeListener.onNegativeClick(v);
getDialog().dismiss();
}
});
return view;
}
效果图 因为如果不设置 padding属性 TextView本身是方角的,会覆盖已经设置好的圆角效果。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在创建iOS框架时如何解决类名冲突?
- ruby-on-rails – Ruby PG gem使用数组作为exec_params的参
- 在C模板类中,我可以使用相同的名称对template参数进行typed
- ios – swift:如何在不使用UIImagePickerController的情况
- react+redux教程(一)connect、applyMiddleware、thunk、w
- c – 合法调用纯粹的虚拟函数
- cocos环境下AndroidStudio安装以及编译APK文件
- Swift - 多线程实现方式(1) - Thread
- 使用wget下载oracle jdk1.8
- objective-c – 为什么在调用自己的类方法时需要使用[self