【问题标题】:Custom view default style not wokring without style tag自定义视图默认样式在没有样式标签的情况下不起作用
【发布时间】:2020-07-03 13:27:47
【问题描述】:

我进行了很多研究并找到了许多有关此问题的来源,但没有适合我的解决方案我不知道出了什么问题。

我研究过的一些资源

Custom view style, android's attributes are ignored

https://androidpedia.net/en/tutorial/1446/creating-custom-views

https://infinum.com/the-capsized-eight/how-to-support-themes-in-custom-views-for-android-apps

这是这个问题的repo

https://github.com/burhankhanzada199888/CustomView-Style-StackOverflow-Question/tree/master/app

我的活动中有 2 个自定义视图,第一个没有样式标签,第二个有样式标签,但 cardView 样式仅在 xml 中使用时适用

custom_view.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:parentTag="com.google.android.material.card.MaterialCardView"
    tools:style="@style/CustomCardView">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="4dp"
        android:background="?colorSurface"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:padding="8dp"
            android:src="@mipmap/ic_launcher"
            app:srcCompat="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="?colorAccent"
            android:gravity="center"
            android:text="Lorem ipsum"
            android:textColor="@android:color/white"
            android:textSize="25sp" />

    </LinearLayout>

</merge>

activity_main.xml

 <com.myapp.CustomCardView
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="1" />

 <com.myapp.CustomCardView
     style="@style/CustomCardView"
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="1" />

CustomCardView.java

public class CustomCardView extends MaterialCardView {

public CustomCardView(Context context) {
    this(context, null);
}

public CustomCardView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

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

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomCardView, defStyleAttr, R.style.CustomCardView);

    float imageSize = a.getDimension(R.styleable.CustomCardView_imageSize, 0);
    float textSize = a.getDimension(R.styleable.CustomCardView_textSize, 0);

    a.recycle();

    inflate(context, R.layout.custom_view, this);

    ImageView imageView = findViewById(R.id.imageView);
    imageView.getLayoutParams().height = (int) imageSize;
    imageView.getLayoutParams().width = (int) imageSize;

    TextView textView = findViewById(R.id.textView);
    textView.setTextSize(textSize);

}

}

styles.xml

<style name="CustomCardView" parent="Widget.MaterialComponents.CardView">
    <item name="cardElevation">8dp</item>
    <item name="cardCornerRadius">16dp</item>
    <item name="android:layout_margin">8dp</item>
    <item name="cardBackgroundColor">?colorPrimary</item>
    <item name="imageSize">150dp</item>
    <item name="textSize">50sp</item>
</style>

attrs.xml

<declare-styleable name="CustomCardView">
    <attr name="imageSize" format="dimension|reference" />
    <attr name="textSize" format="dimension|reference" />
</declare-styleable>

【问题讨论】:

    标签: android android-custom-view material-components-android


    【解决方案1】:

    您可以添加attrs.xml:

    <attr format="reference" name="customCardViewStyle"/>
    

    改变构造函数:

    public CustomCardView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    

    public CustomCardView(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.customCardViewStyle);
    }
    

    然后在你的应用主题中添加:

    <item name="customCardViewStyle">@style/CustomCardView</item>
    

    关注&lt;item name="android:layout_margin"&gt;...&lt;/item&gt;即可。 LayoutParams 特定于父视图类型,您不能将其与主题一起应用。您可以在布局中的 style 中设置布局属性。

    【讨论】:

      【解决方案2】:

      这是因为您在 public CustomCardView(Context context)public CustomCardView(Context context, AttributeSet attrs) 构造函数中删除了超级调用。实际上,您的构造函数必须是这样的:

      public CustomCardView(Context context) {
          super(context)
          this(context, null);
      }
      

      public CustomCardView(Context context, AttributeSet attrs) {
          super(context, attrs)
          this(context, attrs, 0);
      }
      

      【讨论】:

      • IIRC,实际上不需要调用 both 超类的构造函数和自定义类的构造函数。
      猜你喜欢
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      相关资源
      最近更新 更多