Android RecyclerView 行文件中的深色模式文本颜色未更改
Android DarkMode textColor not changed inside RecyclerView's row file
我已经设置了回收站视图,并在 RecyclerView
的行文件中设置了一个 textview
和简单的文本。
我根据选项卡设置了 fragment
,这意味着我使用了 BottomNavigationView
,并且有一个 fragment
包含 RecyclerView
。
并且我在 xml 文件中设置了 android:forceDarkAllowed="true"
。
我应用了 DayNight
主题。
片段 xml 内的 textview
的颜色可以根据深色和浅色主题进行更新,但适配器的项目(用于 RecyclerView
)包含textview
没有改变颜色。
首选。 XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="@dimen/margin_medium"
android:forceDarkAllowed="true"
android:orientation="vertical">
<TextView
style="?attr/textAppearanceHeadline6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_medium"
android:textColor="@color/txt"
android:text="@string/preferences"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
片段:
public class PreferencesFragment extends Fragment {
static final String TAG = "PreferencesFragment";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_preferences, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RecyclerView rvList = view.findViewById(R.id.rvList);
rvList.setLayoutManager(new LinearLayoutManager(DarkThemeApplication.context, RecyclerView.VERTICAL, false));
rvList.setAdapter(new RVListAdapter());
}
class RVListAdapter extends RecyclerView.Adapter<RVListAdapter.ViewHolder> {
@NonNull
@Override
public RVListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,
int viewType) {
return new ViewHolder(LayoutInflater
.from(DarkThemeApplication.context).inflate(R.layout.row_txt, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RVListAdapter.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 50;
}
class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}
}
行文件XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:forceDarkAllowed="true">
<TextView
android:id="@+id/switch1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/txt"
android:layout_margin="@dimen/margin_small"
android:text="asdasdasdas" />
</LinearLayout>
LayoutInflater
.from(DarkThemeApplication.context).inflate(R.layout.row_txt, ...
使用 Activity
上下文而不是 Application
来夸大您的观点。
您的适配器是 Fragment
中的非静态内部 class,因此您只需将 DarkThemeApplication.context
替换为 requireActivity()
。
我已经设置了回收站视图,并在 RecyclerView
的行文件中设置了一个 textview
和简单的文本。
我根据选项卡设置了 fragment
,这意味着我使用了 BottomNavigationView
,并且有一个 fragment
包含 RecyclerView
。
并且我在 xml 文件中设置了 android:forceDarkAllowed="true"
。
我应用了 DayNight
主题。
片段 xml 内的 textview
的颜色可以根据深色和浅色主题进行更新,但适配器的项目(用于 RecyclerView
)包含textview
没有改变颜色。
首选。 XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="@dimen/margin_medium"
android:forceDarkAllowed="true"
android:orientation="vertical">
<TextView
style="?attr/textAppearanceHeadline6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_medium"
android:textColor="@color/txt"
android:text="@string/preferences"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
片段:
public class PreferencesFragment extends Fragment {
static final String TAG = "PreferencesFragment";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_preferences, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RecyclerView rvList = view.findViewById(R.id.rvList);
rvList.setLayoutManager(new LinearLayoutManager(DarkThemeApplication.context, RecyclerView.VERTICAL, false));
rvList.setAdapter(new RVListAdapter());
}
class RVListAdapter extends RecyclerView.Adapter<RVListAdapter.ViewHolder> {
@NonNull
@Override
public RVListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,
int viewType) {
return new ViewHolder(LayoutInflater
.from(DarkThemeApplication.context).inflate(R.layout.row_txt, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RVListAdapter.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 50;
}
class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}
}
行文件XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:forceDarkAllowed="true">
<TextView
android:id="@+id/switch1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/txt"
android:layout_margin="@dimen/margin_small"
android:text="asdasdasdas" />
</LinearLayout>
LayoutInflater .from(DarkThemeApplication.context).inflate(R.layout.row_txt, ...
使用 Activity
上下文而不是 Application
来夸大您的观点。
您的适配器是 Fragment
中的非静态内部 class,因此您只需将 DarkThemeApplication.context
替换为 requireActivity()
。