安卓开发关于字符串加色处理,动态改变布局,遍历View从而让其内部所有子View不可被点击,px转dp处理,TextView的部分处理:
1.字符串加色处理时我们使用SpannableString,并且需要处理多语言下加色文本的起始下标不一样的情况。
方法如下:
代码块一: 加色单个文本,入参是单个key: String
/**
* 设置target的TextView针对不同字段颜色使用
*
* @param target textView
*/
public static void setDifferColor(TextView target, String stringAll, String key, int keyColor) {
if (TextUtils.isEmpty(stringAll)) return;
if (key == null) {
target.setText(stringAll);
return;
}
SpannableString ss = new SpannableString(stringAll);
int startPos;
startPos = stringAll.lastIndexOf(key);
ForegroundColorSpan fcs = new ForegroundColorSpan(keyColor);
ss.setSpan(fcs, startPos, startPos + key.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
target.setText(ss);
}
代码卡二: 加色多个文本, 入参是keyStr: String 【】
/**
* 设置target的TextView针对不同字段颜色使用
*
* @param target textView
*/
public static void setDifferColor(TextView target, String stringAll, String[] keyStr, int keyColor) {
if (TextUtils.isEmpty(stringAll)) return;
if (keyStr == null || keyStr.length == 0) {
target.setText(stringAll);
return;
}
SpannableString ss = new SpannableString(stringAll);
int startPos;
for (String key : keyStr) {
if (!stringAll.contains(key)) {
continue;
}
startPos = stringAll.lastIndexOf(key);
ForegroundColorSpan fcs = new ForegroundColorSpan(keyColor);
ss.setSpan(fcs, startPos, startPos + key.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
target.setText(ss);
}
代码块三: 加色多个文本,分别加色不同颜色,入参是strings : String【】,colors: int【】,颜色和字符串都是数组。
/**
* 设置target的TextView针对不同字段颜色使用
*
* @param target textView
* @param strings 字符串显示数组
* @param colors 颜色显示数组
*/
public static void setDifferColor(TextView target, String[] strings, int[] colors) {
if (strings == null || strings.length == 0 || colors == null || colors.length == 0) return;
StringBuilder str = new StringBuilder();
int[] posSet = new int[strings.length];
int pos = 0;
for (String string : strings) {
str.append(string);
int endPos = string.length();
posSet[pos] = endPos;
pos++;
}
SpannableString ss = new SpannableString(str.toString());
int startPos = 0;
for (int i = 0; i < posSet.length; i++) {
int endPos = startPos + posSet[i];
int color = colors[Math.min(i, colors.length)];
ForegroundColorSpan fcs = new ForegroundColorSpan(color);
ss.setSpan(fcs, startPos, endPos, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
startPos = endPos;
}
target.setText(ss);
}
上述三个代码可以很好解决多语言下字符串有不同的下标情况!
关于上述三个代码块使用如下:
代码块一的使用:
//自动停止文案颜色
fun autoStopTextColor() {
val keyStr = ResUtil.getString(R.string.h7130_auto_stop_heating)
val strAll = ResUtil.getStringFormat(R.string.h7130_auto_stop_heat, keyStr)
StrUtil.setDifferColor(
autoStop, strAll, keyStr,
ResUtil.getColor(R.color.font_style_63_2_textColor)
)
}
代码块二的使用:
String keyStr = ResUtil.getString(R.string.b2light_add_rhythm_path);
String keyStr2 = ResUtil.getString(R.string.scenes_class_timing_rhythm);
String strAll = ResUtil.getStringFormat(R.string.b2light_go_rhythm_tip, keyStr, keyStr2);
StrUtil.setDifferColor(tvTips, strAll, new String[]{keyStr, keyStr2}, ResUtil.getColor(R.color.font_style_63_2_textColor));
代码块三的使用:
private void initStr(int strArrayRes, int colorArrayRes) {
String[] stringArray = getResources().getStringArray(strArrayRes);
int[] colorArray = getResources().getIntArray(colorArrayRes);
StrUtil.setDifferColor(this, stringArray, colorArray);
}
2.遍历View从而让其内部每个View都不可被点击
代码块如下:
/*遍历所有子view,设置enable*/
private fun enableChildView(view: View, enable: Boolean) {
if (view is ViewGroup) {
val childCount = view.childCount
for (i in 0 until childCount) {
val childView = view.getChildAt(i)
childView.isEnabled = enable
enableChildView(childView, enable)
}
}
}
3.px转dp处理, 动态改变布局:
代码块如下:
fun initBindUI(isChange: Boolean) {
if (isChange) {
val lpContentBg = ivContentBg?.layoutParams as PercentRelativeLayout.LayoutParams
val lpSwitchBg = ivSwitchBg?.layoutParams as PercentRelativeLayout.LayoutParams
lpContentBg.percentLayoutInfo.topMarginPercent.percent = 303f / 375
lpSwitchBg.percentLayoutInfo.topMarginPercent.percent = 266f / 375
ivContentBg?.let {
it.layoutParams = lpContentBg
}
ivSwitchBg?.let {
it.layoutParams = lpSwitchBg
}
} else {
ivContentBg?.let {
it.setPadding(it.left, it.top, it.right, it.bottom)
}
ivSwitchBg?.let {
it.setPadding(it.left, it.top, it.right, it.bottom)
}
}
}
4.TextView处理:
处理TextView文本滑动时,不但要在xml布局中加入属性,也要动态加入代码:
代码块如下:
android:scrollbars="vertical"
tvContent = findViewById(R.id.tv_content)
tvContent.movementMethod = ScrollingMovementMethod.getInstance()
tv_content 就是需要垂直方向滑动的文本!
同时写失效标签的时候为了和恒温模式文本顶部对其,使用了
app:layout_constraintTop_toTopOf="@+id/tv_constant_temperature_title"
发现依旧无法和恒温模式文本保持同一高度,还需要加上
app:layout_constraintBottom_toBottomOf="@+id/tv_constant_temperature_title"
同时,wrap_content该失效标签之后,发现标签显示字堆满了整个TextView
这时候需要加入padding值:
<TextView
android:id="@+id/tv_constant_tempe_invalid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:background="@drawable/component_invalid"
android:gravity="center"
android:text="@string/h7130_keep_temperature_invalid"
android:textColor="@color/ui_flag_style_17_textColor"
android:textSize="@dimen/ui_flag_style_17_textSize"
android:visibility="gone"
android:paddingLeft="6.5dp"
android:paddingRight="6.5dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
app:layout_constraintStart_toEndOf="@+id/iv_constant_tempe_remind"
app:layout_constraintTop_toTopOf="@+id/tv_constant_temperature_title"
app:layout_constraintBottom_toBottomOf="@+id/tv_constant_temperature_title"
/>