Здравствуйте, у меня есть простое приложение для Android, в котором у меня есть кнопка. Это тема моего приложения по умолчанию: themes.xml
<resources xmlns:tools = "http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name = "Theme.BaseAppTheme" parent = "Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name = "colorPrimary">@color/purple_500</item>
<item name = "colorPrimaryVariant">@color/purple_700</item>
<item name = "colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name = "colorSecondary">@color/teal_200</item>
<item name = "colorSecondaryVariant">@color/teal_700</item>
<item name = "colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name = "android:statusBarColor" tools:targetApi = "l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<style name = "Theme.WalkTest" parent = "Theme.BaseAppTheme">
<item name = "android:textColor">@android:color/black</item>
</style>
</resources>
В отдельном файле button.xml я определил тему для своей кнопки:
<?xml version = "1.0" encoding = "utf-8"?>
<resources>
<style name = "Theme.WalkTest.ButtonSecondary" parent = "Theme.WalkTest">
<item name = "android:backgroundTint">@android:color/holo_green_dark</item>
<item name = "android:textColor">@android:color/black</item>
</style>
</resources>
а вот кнопка у меня в фрагменте: fragment_buildings.xml (использую навигацию).
<Button
android:id = "@+id/create_building_button"
android:layout_width = "0dp"
android:layout_height = "wrap_content"
android:layout_marginStart = "10dp"
android:layout_marginTop = "8dp"
android:layout_marginEnd = "10dp"
android:drawableLeft = "@drawable/ic_add"
android:paddingLeft = "15dp"
android:paddingTop = "20dp"
android:paddingRight = "15dp"
android:paddingBottom = "20dp"
android:text = "@string/new_building"
android:textAlignment = "textStart"
android:theme = "@style/Theme.WalkTest.ButtonSecondary"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintHorizontal_bias = "1.0"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toBottomOf = "@+id/toolbar" />
Вот предварительный просмотр студии Android:
Я вижу, что смена фона работает, но textColor не меняется?
Я могу использовать атрибуты элемента кнопки, чтобы изменить его цвет, но мне нужны повторно используемые стили.
Это моя версия Android Studio:
Android Studio 4.1
Build #AI-201.8743.12.41.6858069, built on September 23, 2020
Runtime version: 1.8.0_242-release-1644-b3-6222593 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.15.7
GC: ParNew, ConcurrentMarkSweep
Memory: 2014M
Cores: 4
Registry: ide.new.welcome.screen.force=true
Non-Bundled Plugins: IdeaVIM, org.jetbrains.kotlin
использовать
style = "@style/Theme.WalkTest.ButtonSecondary"
вместо
android:theme = "@style/Theme.WalkTest.ButtonSecondary"
Во-первых, определите свои стили в values/styles.xml
Пример:
<resources xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools">
<style name = "text_h3">
<item name = "android:textSize">14sp</item>
<item name = "android:textColor">@color/colorPrimary</item>
<item name = "android:fontFamily">@font/font_medium</item>
<item name = "android:clickable">true</item>
<item name = "android:focusable">true</item>
<item name = "android:background">?android:attr/selectableItemBackground</item>
</style>
</resource>
Во-вторых, используйте определенный стиль для требуемого представления,
Пример:
<TextView
android:id = "@+id/tv_name_label"
style = "@style/text_h3"
...
</TextView>
Это один из правильных способов укладки, надеюсь, он вам помог.
@homerman нет, на самом деле все в порядке. Я просто хотел определить стили и применить их, ха-ха.
Это пример применения стиля непосредственно к
View. Однако вопрос заключался в применении стилей через тему.