gpt4 book ai didi

android - 在 RecyclerView 中未正确解析暗模式颜色

转载 作者:行者123 更新时间:2023-12-04 02:38:23 25 4
gpt4 key购买 nike

我正在尝试实现夜间模式,但我遇到了问题:CardView不会将颜色更改为 night值,但 Activity 正确地将其背景更改为夜间颜色值。



请帮助理解问题。

MainActivity

public class NewFinishActivity extends AppCompatActivity {

@BindView(R.id.finishRecycler)
RecyclerView finishRecycler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

setContentView(R.layout.activity_new_finish);
ButterKnife.bind(this);


LevelRecyclerAdapter mAdapter = new LevelRecyclerAdapter(getApplicationContext(), new ArrayList<>());
finishRecycler.setAdapter(mAdapter);
}
}

MainActivity布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
tools:context=".activities.NewFinishActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/finishRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_color"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</FrameLayout>

适配器
public class LevelRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<UserLevel> mItems;
private Context mContext;

public LevelRecyclerAdapter(Context context, List<UserLevel> items) {
mContext = context;
mItems = items;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
AbstractCard card;
card = new LevelCardView(mContext, parent);
return card;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
if (!mItems.isEmpty()) {
UserLevel data = mItems.get(position);
LevelCardView card = (LevelCardView) viewHolder;
card.bind(data);
}
}

@Override
public int getItemCount() {
return 5;
}

public void update(List<UserLevel> items) {
mItems = items;
notifyDataSetChanged();
}
}

RecyclerView.ViewHolder
public class LevelCardView extends AbstractCard {

Context mContext;
@BindView(R.id.title)
TextView title;
@BindView(R.id.levelCheck)
AppCompatImageView levelCheck;
@BindView(R.id.levelCard)
CardView levelCard;

public LevelCardView(Context context, ViewGroup parent) {
this(context, LayoutInflater.from(context).inflate(R.layout.card_level, parent, false));
}

private LevelCardView(Context context, View view) {
super(view, context);
mContext = context;
try {
ButterKnife.bind(this, view);
} catch (Exception e) {
e.printStackTrace();
}
}

RecyclerView.ViewHolder xml
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/levelCard"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardBackgroundColor="@color/background_color_card"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="@dimen/card_evalation">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:padding="15dp">

<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableLeft="@drawable/ic_goal_1"
android:fontFamily="@font/roboto"
android:gravity="center"
android:text="Lose Fat"
android:textColor="@color/textColorStandard"
android:textSize="20dp" />

</RelativeLayout>

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/levelCheck"
android:layout_width="70dp"
android:visibility="gone"
android:layout_height="match_parent"
android:layout_gravity="end"
android:scaleType="centerCrop"
app:srcCompat="@drawable/gender_selected" />
</FrameLayout>

</androidx.cardview.widget.CardView>

colors.xml
<color name="textColorStandard">#282b35</color>
<color name="background_color">#fdfdfdfd</color>
<color name="background_color_card">#fff</color>
<color name="textColorStandardTransparent">#B222273D</color>

colors.xml晚上
<color name="textColorStandard">#FCFCFC</color>
<color name="background_color">#14182A</color>
<color name="background_color_card">#2F3550</color>
<color name="textColorAlwaysStandard">#282b35</color>

最佳答案

您使用的上下文不正确。您应该使用 Activity 的上下文,而不是使用应用程序的上下文。

而不是执行:

card = new LevelCardView(mContext, parent);

执行以下操作:
card = new LevelCardView(parent.getContext(), parent);

关于android - 在 RecyclerView 中未正确解析暗模式颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60605064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com