近期开发项目,做个按钮点击弹出展示微信二维码关注微信公众号的展示框,于是想到DialogFragment,期间对Dialog的title的底框字体颜色做出相关设置,记录如下:
下面是 DialogFragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/wechat"
android:layout_marginBottom="5dp"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关注后提醒用户登录账户"
android:textColor="#e70012"
android:textSize="18dp"
android:layout_marginBottom="20dp"/>
</LinearLayout>
DialogFragment.java:
public class FragmentDialogWeChatImage extends DialogFragment{ @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View mView=inflater.inflate(R.layout.fragment_dialog_wechat_image,container,false);
getDialog().setTitle("扫码关注微信公众号");//设置Dialog字体颜色
TextView title=(TextView) getDialog().findViewById(android.R.id.title);
title.setTextColor(getResources().getColor(R.color.red));//设置字体颜色
title.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);//设置字体水平居中 int titleDividerId = getResources().getIdentifier("titleDivider", "id", "android");
View titleDivider = getDialog().findViewById(titleDividerId);
if (titleDivider != null) {
titleDivider.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));//设置标题底线color
}
return mView;
} }
点击按钮实现弹出DialogFragment:
/**
* 点击关联微信
*/
@OnClick(R.id.txt_relation_wechat)
public void onClickWechat(View view){
FragmentDialogWeChatImage fdw=new FragmentDialogWeChatImage();
FragmentTransaction ft=getFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fdw.show(ft,"fdw");
}