Пользовательский стиль по умолчанию не работает без тега стиля - PullRequest
0 голосов
/ 23 марта 2020

Я много исследовал и нахожу много источников этой проблемы, но у меня нет решений, я не знаю, что идет не так.

Некоторые ресурсы, которые я изучил,

Пользовательский стиль просмотра, атрибуты Android игнорируются

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

Это репозиторий для этой проблемы

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

Сначала у меня есть 2 пользовательских представления в моей деятельности без тега стиля и второй с тегом стиля, но стиль cardView применяется только при использовании в xml

main_activity

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);

}

}

стили. 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>

1 Ответ

0 голосов
/ 23 марта 2020

Это происходит потому, что вы удалили супер-вызовы в конструкторах 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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...