属性
isIndicatorRatingBar 是否为指示器,为true时,用户将无法交互操作,默认为false。
numStars 显示的星型数量,必须是一个整形值,像“50”,虽然可以设置很大,但一般都是5-10个星星即可。
rating 设置默认的评分。
stepSize 评分每次增加的值。建议大于0小于等于1之间最合适。
样式
其中内置了三个样式:
style="?attr/ratingBarStyle":默认样式
style="?android:attr/ratingBarStyleSmall":小样式
style="?android:attr/ratingBarStyleIndicator":指示器样式
dialog弹框显示好评操作效果图
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/x582"
android:layout_height="wrap_content"
android:background="@drawable/shape_while_radius_10dp_style"
android:minHeight="@dimen/y280"
android:orientation="vertical"
android:padding="@dimen/dp_20">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="@dimen/dp_6"
android:text="@string/司机评分"
android:textColor="@color/color_333333"
android:textSize="@dimen/sp_16"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/dp_6">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/事故" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dp_30"
android:layout_marginEnd="@dimen/dp_30"
android:layout_weight="1"
android:background="@drawable/shape_color_remakes_label_style"
android:gravity="center_horizontal">
<RatingBar
android:id="@+id/mRtbAccident"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:isIndicator="false"
android:numStars="5"
android:paddingTop="@dimen/dp_2"
android:paddingBottom="@dimen/dp_2"
android:stepSize="1"
android:theme="@style/RatingBar" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/dp_6">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/违章" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dp_30"
android:layout_marginEnd="@dimen/dp_30"
android:layout_weight="1"
android:background="@drawable/shape_color_remakes_label_style"
android:gravity="center_horizontal">
<RatingBar
android:id="@+id/mRtbRegulations"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:isIndicator="false"
android:numStars="5"
android:paddingTop="@dimen/dp_2"
android:paddingBottom="@dimen/dp_2"
android:stepSize="1"
android:theme="@style/RatingBar" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/dp_6">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/违纪" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dp_30"
android:layout_marginEnd="@dimen/dp_30"
android:layout_weight="1"
android:background="@drawable/shape_color_remakes_label_style"
android:gravity="center_horizontal">
<RatingBar
android:id="@+id/mRtbDiscipline"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:isIndicator="false"
android:numStars="5"
android:paddingTop="@dimen/dp_2"
android:paddingBottom="@dimen/dp_2"
android:stepSize="1"
android:theme="@style/RatingBar" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/dp_6"
android:text="@string/评分备注" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_10"
android:layout_marginRight="@dimen/dp_10"
android:background="@drawable/shape_color_remakes_label_style"
android:minHeight="@dimen/dp_60"
android:padding="@dimen/dp_6">
<EditText
android:id="@+id/mEtRemarks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:gravity="top|left"
android:hint="@string/这是评分备注"
android:inputType="textMultiLine"
android:maxLines="5"
android:minLines="3"
android:textSize="@dimen/sp_13" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="@dimen/dp_6"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal"
android:padding="@dimen/dp_5">
<TextView
android:id="@+id/mTvCancelDialog"
android:layout_width="@dimen/x150"
android:layout_height="@dimen/y64"
android:layout_marginRight="@dimen/x30"
android:background="@drawable/shape_247eff_30_stroke_style"
android:gravity="center"
android:text="取消"
android:textColor="@color/color_247EFF"
android:textSize="@dimen/sp_14"
android:textStyle="bold" />
<TextView
android:id="@+id/mTvConfirmDialog"
android:layout_width="@dimen/x150"
android:layout_height="@dimen/y64"
android:layout_marginLeft="@dimen/x30"
android:background="@drawable/shape_247eff_radius_30dp_style"
android:gravity="center"
android:text="确认"
android:textColor="@color/color_FFFFFF"
android:textSize="@dimen/sp_14"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
drawable ----> shape_while_radius_10dp_style.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/color_FFFFFF" />
<corners android:radius="@dimen/x20" />
</shape>
drawable -----> shape_color_remakes_label_style.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/color_FAFAFA"/>
<corners android:radius="@dimen/dp_5"/>
<stroke android:color="@color/color_F3F3F3"
android:width="@dimen/dp_1"/>
<corners android:radius="@dimen/dp_5"/>
</shape>
values ------> styles.xml
RatingBar颜色:
<!--RatingBar颜色-->
<style name="RatingBar" parent="Theme.AppCompat">
<item name="colorControlNormal">@color/color_CCCCCC</item>
<item name="colorControlActivated">@color/color_FF7070</item>
</style>
Dialog主题:
<style name="CenterDialogTheme" parent="@android:style/Theme.Dialog">
<!-- 边框 -->
<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>
<item name="android:background">@android:color/transparent</item>
<!-- 背景透明 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 模糊 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 遮罩层 -->
<item name="android:backgroundDimAmount">0.5</item>
</style>
drawable -----> shape_247eff_30_stroke_style.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="@dimen/x50"/>
<stroke android:color="@color/color_247EFF" android:width="@dimen/x2"/>
</shape>
drawable -----> shape_247eff_radius_30dp_style.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- rectangle表示为矩形 -->
<!-- 填充的颜色 -->
<solid android:color="@color/color_247EFF" />
<!-- android:radius 圆角的半径 -->
<corners android:radius="30dp" />
</shape>
CenterDialogView:
public class CenterDialogView {
private static CenterDialogView mInstance;
private Context mContext;
private Dialog mDialog;
private static final String TAG = "CenterDialogViewTag";
private long exitTime = 0;
private CenterDialogView(Context context) {
this.mContext = context;
createDialog(context);
}
private void createDialog(Context context) {
//1、使用Dialog、设置style
mDialog = new Dialog(context, R.style.CenterDialogTheme);
mDialog.setCancelable(true);
//设置setCancelable为false之后,物理返回按键不能取消弹框。需要设置该监听,以达到按返回键取消弹框的目的
mDialog.setOnKeyListener((dialog, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
if ((System.currentTimeMillis() - exitTime) > 2000) {
exitTime = System.currentTimeMillis();
} else {
mDialog.cancel();
}
return true;
}
return false;
});
}
public static CenterDialogView getInstance(Context context) {
if (mInstance == null) {
synchronized (CenterDialogView.class) {
if (mInstance == null) {
mInstance = new CenterDialogView(context);
}
}
}
return mInstance;
}
public Dialog loadDialogView(View view, float widthPx, float heightPx) {
//2、设置布局
mDialog.setContentView(view);
Window window = mDialog.getWindow();
//设置对话框大小
window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
WindowManager.LayoutParams layoutParams = mDialog.getWindow().getAttributes();
layoutParams.width = (int) widthPx;
layoutParams.height = (int) heightPx;
if (heightPx == 0) {
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
}
mDialog.getWindow().setAttributes(layoutParams);
//设置弹出位置
window.setGravity(Gravity.CENTER);
return mDialog;
}
public void showDialog() {
if (!mDialog.isShowing()) {
mDialog.show();
}
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*
* @param dpValue 尺寸dip
* @return 像素值
*/
private int dp2px(float dpValue) {
final float scale = mContext.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public void cancelDialog() {
if (mDialog != null) {
mDialog.cancel();
}
}
/**
* 评论弹框
*
* @return
*/
public View CommentDriverDialog(OnCommentDriverListener onCommentDriverListener) {
View view = LayoutInflater.from(mContext).inflate(R.layout.comment_task_dialog, null, false);
RatingBar mRtbAccident = view.findViewById(R.id.mRtbAccident);
RatingBar mRtbRegulations = view.findViewById(R.id.mRtbRegulations);
RatingBar mRtbDiscipline = view.findViewById(R.id.mRtbDiscipline);
EditText mEtRemarks = view.findViewById(R.id.mEtRemarks);
TextView mTvCancelDialog = view.findViewById(R.id.mTvCancelDialog);
TextView mTvConfirmDialog = view.findViewById(R.id.mTvConfirmDialog);
mRtbAccident.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Log.i("onRatingChangedTAG", "rating: " + rating + ", fromUser:" + fromUser);
}
});
view.findViewById(R.id.mTvCancelDialog).setOnClickListener(v -> {
if (mDialog != null) mDialog.cancel();
});
view.findViewById(R.id.mTvConfirmDialog).setOnClickListener(v -> {
if (onCommentDriverListener != null) onCommentDriverListener.onClick();
});
return view;
}
public interface OnCommentDriverListener {
void onClick();
}
public void destroyView() {
Log.d(TAG, "destroyView: ");
if (mDialog != null) {
mDialog.cancel();
mDialog = null;
}
if (mInstance != null) {
mInstance = null;
}
}
}