2

我已经设置了回收站视图,并textviewRecyclerView.

我已经fragment根据标签设置了我已经使用了的BottomNavigationView,并且有一个fragment包含RecyclerView.

而且我已经设置android:forceDarkAllowed="true"了xml文件里面。

我已经应用了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>
4

1 回答 1

1
LayoutInflater
            .from(DarkThemeApplication.context).inflate(R.layout.row_txt, ...

使用Activity上下文而不是使用上下文Application来夸大您的观点。

您的适配器是 a 中的非静态内部类,Fragment因此您只需替换DarkThemeApplication.contextrequireActivity().

于 2019-10-24T09:37:56.243 回答