如何动态更改 cardview 边框颜色?

How can I change the cardview border color dynamically?

我想根据 content.Is 更改我的 cardview 的边框颜色是否可以从 recyclerview 适配器以某种方式访问​​ xml 文件并更改颜色?

xml file from the recyclerview adapter and change the color?

没有。 XML 是只读的。而且你不需要去碰它,而是使用i.f。 findViewById(),找到你的卡片视图,并使用它的方法来改变颜色。

这可以通过使用 Databinding 库来实现。
假设您的内容是 User,如果 he/she 是成人或儿童,您需要更改 CardView 的颜色。因此,您可以像这样在 Activity 或 Fragment 中传递对象:

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
   User user = new User("Test", "User");
   binding.setUser(user);
}

然后,在 XML 文件中添加 data 标签和变量:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>

   <android.support.v7.widget.CardView
     android:background="@{user.isAdult ? @color/yellow : @color/gray }"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.firstName}"/>
   </android.support.v7.widget.CardView>
</layout>

如果您想了解更多信息,请查看文档:https://developer.android.com/topic/libraries/data-binding/index.html?hl=pt-br