【问题标题】:RecyclerView styling at theme level主题级别的 RecyclerView 样式
【发布时间】:2016-12-28 20:03:30
【问题描述】:

我正在尝试在 主题 级别为RecyclerView 实现通用样式参数。与ListView 相比,我使用了这样的东西:

定义了一个样式:

<style name="StyleListView" parent="Widget.AppCompat.ListView">
     <item name="android:requiresFadingEdge">vertical</item>
     <item name="android:fadingEdgeLength">10dp</item>
     <item name="android:scrollbars">vertical</item>
</style>

后来在我的自定义主题中使用:

<item name="android:listViewStyle">@style/StyleListView</item>

这对ListView 非常有效。但是,我无法为 RecyclerView 反映这一点,因为我认为它适用于任何类型的列表。

那么,RecyclerView 是否有任何预定义的样式属性可用,例如 android:recyclerViewStyle 或其他什么?

如果没有,那么我如何在主题级别实现这一点?

【问题讨论】:

标签: android listview styles android-recyclerview themes


【解决方案1】:

不幸的是,RecylerView 没有与 listViewStyle 等效的版本。

我认为您能做的最好的事情就是为您的RecyclerView 定义一个样式,然后让您想要使用该样式的任何视图都使用style="@style/RecyclerViewStyle"(如果您只想定义几个属性,例如您的示例)。

如果您真的不想对每个RecyclerView 都这样做,那么您必须对其进行子类化并在构造函数中为defStyle 返回一个非零参数。不过,您必须用新的子类替换 XML 中 RecyclerView 的所有实例。

【讨论】:

    【解决方案2】:

    正如@Jason Robinson 所说,您可以通过这种方式继承 RecyclerView,将 xml 中的 RecyclerView 替换为 RecyclerViewStyleable,然后使用 recyclerViewStyle 对其进行样式化

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="RecyclerViewStyleable">
            <attr name="recyclerViewStyle"/>
        </declare-styleable>
    </resources>
    

    -

    public class RecyclerViewStyleable extends RecyclerView {
        public RecyclerViewStyleable(Context context) {
            super(context);
        }
    
        public RecyclerViewStyleable(Context context, AttributeSet attrs) {
            this(context, attrs, R.attr.recyclerViewStyle);
        }
    
        public RecyclerViewStyleable(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    }
    

    【讨论】:

      【解决方案3】:

      主题级别的 RecyclerView 样式

      RecyclerView 现在有一个默认样式属性:recyclerViewStyle,它允许在您的主题中设置默认样式

      支持以下属性

          <!-- Class name of the Layout Manager to be used. -->
          <attr name="layoutManager" format="string" />
         
          <!-- ============================= -->
          <!-- Attributes for Layout Manager -->
          <!-- ============================= -->
          <attr name="android:orientation" />
          <attr name="android:descendantFocusability" />
          <attr name="android:clipToPadding" />
          <attr name="spanCount" format="integer"/>
          <attr name="reverseLayout" format="boolean" />
          <attr name="stackFromEnd" format="boolean" />
          <attr name="fastScrollEnabled" format="boolean" />
          <attr name="fastScrollVerticalThumbDrawable" format="reference" />
          <attr name="fastScrollVerticalTrackDrawable" format="reference" />
          <attr name="fastScrollHorizontalThumbDrawable" format="reference" />
          <attr name="fastScrollHorizontalTrackDrawable" format="reference" />
      

      要使用上述功能,请在 build.gradle 文件中添加/更新以下依赖项。

      dependencies {
          implementation 'androidx.recyclerview:recyclerview:1.1.0-beta05'
      }
      

      你可以看到提交here

      示例:

      1.定义您的默认样式

      <style name="DefaultRecyclerViewStyle">
      
          <item name="layoutManager">androidx.recyclerview.widget.GridLayoutManager</item>
          <item name="android:orientation">vertical</item>
          <item name="spanCount">2</item>
      
      </style>
      

      2。将上述样式添加到您的默认主题中

      <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
          <!-- Customize your theme here. -->
          <item name="colorPrimary">@color/colorPrimary</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
          <item name="colorAccent">@color/colorAccent</item>
          <item name="recyclerViewStyle">@style/DefaultRecyclerViewStyle</item>
      </style>
      

      就是这样。您已经为 RecyclerView 小部件定义了应用程序范围的默认样式。

      【讨论】:

        【解决方案4】:

        根据this changelog 2019 年8 月7 日,RecyclerView 添加了recyclerViewStyle 样式属性,其行为类似于listViewStyle

        将其添加到您的build.gradle

        dependencies {
            ...
            implementation 'androidx.recyclerview:recyclerview:1.1.0-beta02'
            ...
        }
        

        注意:您需要使用 AndroidX,它是 Android Jetpack. 的一部分

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-09-20
          • 1970-01-01
          • 2013-07-13
          • 1970-01-01
          • 1970-01-01
          • 2023-03-29
          • 1970-01-01
          • 2020-12-08
          相关资源
          最近更新 更多