【问题标题】:How to add CardView attributes to app theme?如何将 CardView 属性添加到应用程序主题?
【发布时间】:2015-04-28 08:51:33
【问题描述】:

我的问题类似于“How to put a CardView attribute in a style”,但我需要更深入。

我正在使用 AppCompat 主题,我的样式看起来像

style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/toolbar_color</item>
    <item name="android:listViewStyle">@style/CustomListView</item>
</style>

我为 CardView 创建单独的样式

<style name="CustomCardView" parent="CardView">
    <item name="cardBackgroundColor">@color/card_background</item>
    <item name="cardCornerRadius">@dimen/card_corner</item>
</style>

我可以将它附加到主要样式吗?

【问题讨论】:

标签: android android-cardview


【解决方案1】:
public View(Context context, AttributeSet attrs, int defStyleAttr)

@param defStyleAttr 当前主题中的一个属性,包含一个 引用提供默认值的样式资源 风景。可以为 0 以不查找默认值。

所以你必须这样做

attrs.xml

<attr name="myCardViewStyle"/>

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="myCardViewStyle">@style/CustomCardView</item>
</style>

<style name="CustomCardView" parent="CardView">
    <item name="cardBackgroundColor">@android:color/holo_red_dark</item>
</style>

MyCardView.java

public MyCardView(Context context, AttributeSet attrs) {
    this(context, attrs, R.attr.myCardViewStyle);
}

public MyCardView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

就是这样。

【讨论】:

  • 您能解释一下在代码或样式中在哪里引用 MyCardView 类吗?
  • @SenthilkumarS 只需在您的布局文件中使用它,就像任何其他视图一样。 &lt;com.myapplicationpackage.views.custom.MyCardView&gt; ...&lt;/com.myapplicationpackage.views.custom.MyCardView&gt;
猜你喜欢
  • 2016-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多