Android 从 Adapter 的 TextView 中设置 Drawable 背景 API 21
Android set Drawable background in TextView from Adapter for API 21
我面临从回收适配器 holder
以编程方式为 API 21.
更改颜色或可绘制资源的问题
在下面的代码中,我正在更改 blow API23 的可绘制资源,但没有任何反应。我尝试更改可绘制背景颜色,但均无效。
I would prefer to change only background tint if possible for API 21
so I can avoid using extra drawable but if not possible than I can use
one.
注意: 对于默认样式,我在<style>
中设置了TextView背景(可绘制)。
RecyclerView 适配器
@Override
public void onBindViewHolder(@NonNull StepHolder holder, int position) {
Step step = mStep.get(position);
holder.setCount(step.getCount());
holder.setDescription(step.getDescription());
if (position <= 2) {
if (Build.VERSION.SDK_INT < 23) {
// change drawable color property
holder.count.setBackgroundResource(R.drawable.shape_circle_accent);
} else {
holder.count.setBackgroundTintList(mContext.getColorStateList(R.color.colorAccent));
}
}
}
ViewHolder
public class StepHolder extends RecyclerView.ViewHolder {
public TextView count, description;
public View timeline;
public StepHolder(@NonNull View itemView) {
super(itemView);
this.count = itemView.findViewById(R.id.v_step_count);
this.description = itemView.findViewById(R.id.v_step_description);
}
public void setCount(String count) {
this.count.setText(count);
}
public void setDescription(String description) {
this.description.setText(description);
}
public void setTimeline(View timeline) {
this.timeline = timeline;
}
}
模态
public class Step {
private String count, description;
public Step(String count, String description) {
this.count = count;
this.description = description;
}
public String getCount() {
return count;
}
public String getDescription() {
return description;
}
@Override
public String toString() {
return "Step{" +
"count='" + count + '\'' +
", description='" + description + '\'' +
'}';
}
}
查看
<TextView
android:id="@+id/v_step_count"
style="@style/StepsCounterTheme"
android:layout_marginStart="@dimen/app_space"
android:layout_marginEnd="@dimen/app_space"
android:text="@string/_1"
app:layout_constraintBottom_toTopOf="@+id/v_step_timeline_path"
app:layout_constraintEnd_toStartOf="@+id/v_step_description"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread" />
可绘制
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/colorWhite" />
</shape>
风格
Here I have realized the issue is with the style. Means if I set the
backgroundTint
in style, the code for API < 23 won't affect. Once I
have tried removing backgroundTint
from style. The code is working.
<!-- steps to prepare timeline counter theme -->
<style name="StepsCounterTheme" parent="TextAppearance.MaterialComponents.Body1">
<item name="android:layout_width">@dimen/app_space_ot</item>
<item name="android:layout_height">@dimen/app_space_ot</item>
<item name="android:background">@drawable/shape_circle</item>
<item name="android:backgroundTint">@color/colorPrimary</item> <!-- this one -->
<item name="android:textColor">@color/colorWhite</item>
<item name="android:gravity">center</item>
</style>
Question
So now I wonder how to set the default backgroundTint
in style? Do I
have to create version specific style? Will that allow overriding in
Java for API < 23?
试试这个,
private void setBackgroundColor (TextView tv){
GradientDrawable bgShape = (GradientDrawable) tv.getBackground();
bgShape.mutate();
bgShape.setColor(ContextCompat.getColor(mContext.getApplicationContext(), R.color.colorAccent));
}
Usage
if (Build.VERSION.SDK_INT < 23) {
setBackgroundColor(holder.count);
} else {
holder.count.setBackgroundTintList(mContext.getColorStateList(R.color.colorAccent));
}
不知道我有没有看懂问题。哈哈..
试试这个
holder.count.setBackground(mContext.getResources().getDrawable(R.drawable.shape_circle_accent));
holder.count.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.colorD));
我面临从回收适配器 holder
以编程方式为 API 21.
在下面的代码中,我正在更改 blow API23 的可绘制资源,但没有任何反应。我尝试更改可绘制背景颜色,但均无效。
I would prefer to change only background tint if possible for API 21 so I can avoid using extra drawable but if not possible than I can use one.
注意: 对于默认样式,我在<style>
中设置了TextView背景(可绘制)。
RecyclerView 适配器
@Override
public void onBindViewHolder(@NonNull StepHolder holder, int position) {
Step step = mStep.get(position);
holder.setCount(step.getCount());
holder.setDescription(step.getDescription());
if (position <= 2) {
if (Build.VERSION.SDK_INT < 23) {
// change drawable color property
holder.count.setBackgroundResource(R.drawable.shape_circle_accent);
} else {
holder.count.setBackgroundTintList(mContext.getColorStateList(R.color.colorAccent));
}
}
}
ViewHolder
public class StepHolder extends RecyclerView.ViewHolder {
public TextView count, description;
public View timeline;
public StepHolder(@NonNull View itemView) {
super(itemView);
this.count = itemView.findViewById(R.id.v_step_count);
this.description = itemView.findViewById(R.id.v_step_description);
}
public void setCount(String count) {
this.count.setText(count);
}
public void setDescription(String description) {
this.description.setText(description);
}
public void setTimeline(View timeline) {
this.timeline = timeline;
}
}
模态
public class Step {
private String count, description;
public Step(String count, String description) {
this.count = count;
this.description = description;
}
public String getCount() {
return count;
}
public String getDescription() {
return description;
}
@Override
public String toString() {
return "Step{" +
"count='" + count + '\'' +
", description='" + description + '\'' +
'}';
}
}
查看
<TextView
android:id="@+id/v_step_count"
style="@style/StepsCounterTheme"
android:layout_marginStart="@dimen/app_space"
android:layout_marginEnd="@dimen/app_space"
android:text="@string/_1"
app:layout_constraintBottom_toTopOf="@+id/v_step_timeline_path"
app:layout_constraintEnd_toStartOf="@+id/v_step_description"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread" />
可绘制
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/colorWhite" />
</shape>
风格
Here I have realized the issue is with the style. Means if I set the
backgroundTint
in style, the code for API < 23 won't affect. Once I have tried removingbackgroundTint
from style. The code is working.
<!-- steps to prepare timeline counter theme -->
<style name="StepsCounterTheme" parent="TextAppearance.MaterialComponents.Body1">
<item name="android:layout_width">@dimen/app_space_ot</item>
<item name="android:layout_height">@dimen/app_space_ot</item>
<item name="android:background">@drawable/shape_circle</item>
<item name="android:backgroundTint">@color/colorPrimary</item> <!-- this one -->
<item name="android:textColor">@color/colorWhite</item>
<item name="android:gravity">center</item>
</style>
Question
So now I wonder how to set the default
backgroundTint
in style? Do I have to create version specific style? Will that allow overriding in Java for API < 23?
试试这个,
private void setBackgroundColor (TextView tv){
GradientDrawable bgShape = (GradientDrawable) tv.getBackground();
bgShape.mutate();
bgShape.setColor(ContextCompat.getColor(mContext.getApplicationContext(), R.color.colorAccent));
}
Usage
if (Build.VERSION.SDK_INT < 23) {
setBackgroundColor(holder.count);
} else {
holder.count.setBackgroundTintList(mContext.getColorStateList(R.color.colorAccent));
}
不知道我有没有看懂问题。哈哈..
试试这个
holder.count.setBackground(mContext.getResources().getDrawable(R.drawable.shape_circle_accent));
holder.count.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.colorD));