为什么不能应用thems.xml中的按钮样式?
Why can't the button style in thems.xml be applied?
我现在正在尝试将新字体应用到 entire application
。
顺便说一句,虽然在themes.xml
中定义了style tag
,但是textView
是集体应用的,但是字体并没有应用到按钮和芯片等其他视图.
只好直接style=""
按钮了,太麻烦了
而且样式不能直接应用到Chip
。
这是什么原因?这是一个错误吗?
themes.xml
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Base application theme. -->
<style name="Theme.WriteWeight" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/light_green_dark</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. -->
<item name="android:textViewStyle">@style/TextViewFont</item>
<item name="android:buttonStyle">@style/ButtonStyle</item>
<item name="chipStyle">@style/ChipStyle</item>
</style>
<style name="TextViewFont" parent="@android:style/Widget.DeviceDefault.TextView">
<item name="android:fontFamily">@font/roboto_font_family</item>
<item name="android:includeFontPadding">false</item>
</style>
<style name="ButtonStyle" parent="@android:style/Widget.DeviceDefault.Button">
<item name="android:fontFamily">@font/roboto_regular</item>
<item name="android:includeFontPadding">false</item>
<item name="backgroundTint">@color/light_green</item>
</style>
<style name="ChipStyle" parent="Widget.MaterialComponents.Chip.Choice">
<item name="android:fontFamily">@font/roboto_regular</item>
<item name="android:includeFontPadding">false</item>
</style>
</resources>
roboto_font_family.xml
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- thin -->
<font
android:font="@font/roboto_thin"
android:fontStyle="normal"
android:fontWeight="100"
app:font="@font/roboto_thin"
app:fontStyle="normal"
app:fontWeight="100"/>
<!-- light -->
<font
android:font="@font/roboto_light"
android:fontStyle="normal"
android:fontWeight="300"
app:font="@font/roboto_light"
app:fontStyle="normal"
app:fontWeight="300"/>
<!-- regular -->
<font
android:font="@font/roboto_regular"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/roboto_black"
app:fontStyle="italic"
app:fontWeight="400" />
</font-family>
fragment_body_part_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:backgroundTint="@color/light_green">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border_dialog">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcy_part"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
tools:listitem="@layout/body_part_item"/>
</FrameLayout>
<Button
android:id="@+id/check"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="start"
android:textSize="27dp"
android:backgroundTint="@color/light_green"/>
</LinearLayout>
fragment_workout_list_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<HorizontalScrollView
android:id="@+id/horizontal_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/routine_list_recycler"
android:scrollbars="none"
android:overScrollMode="never">
<com.google.android.material.chip.ChipGroup
android:id="@+id/routine_chipgroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
app:chipSpacingHorizontal="10dp"
app:singleLine="true"
app:singleSelection="true"
style="@style/ChipStyle">
<com.google.android.material.chip.Chip
android:id="@+id/chip_back"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"
android:text="BACK"
app:rippleColor="@color/light_green"
style="@style/CustomChipChoice"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_chest"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"
android:text="CHEST"
app:rippleColor="@color/light_green"
style="@style/CustomChipChoice"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_leg"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"
android:text="LEG"
app:rippleColor="@color/light_green"
style="@style/CustomChipChoice"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_sholuder"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"
android:text="SHOULDER"
app:rippleColor="@color/light_green"
style="@style/CustomChipChoice"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_biceps"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"
android:text="BICEPS"
app:rippleColor="@color/light_green"
style="@style/CustomChipChoice"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_triceps"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"
android:text="TRICEP"
app:rippleColor="@color/light_green"
style="@style/CustomChipChoice"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_abs"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"
android:text="ABS"
app:rippleColor="@color/light_green"
style="@style/CustomChipChoice"/>
</com.google.android.material.chip.ChipGroup>
</HorizontalScrollView>
<View
android:id="@+id/horizontal_bar"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/light_green"
app:layout_constraintTop_toBottomOf="@id/horizontal_scroll"
app:layout_constraintBottom_toTopOf="@id/routine_list_recycler"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/routine_list_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/horizontal_bar" />
</androidx.constraintlayout.widget.ConstraintLayout>
您可以在 Material 主题中设置按钮样式:
<item name="materialButtonStyle">@style/ButtonStyle</item>
<style name="ButtonStyle" parent="Widget.MaterialComponents.Button">
...
我现在正在尝试将新字体应用到 entire application
。
顺便说一句,虽然在themes.xml
中定义了style tag
,但是textView
是集体应用的,但是字体并没有应用到按钮和芯片等其他视图.
只好直接style=""
按钮了,太麻烦了
而且样式不能直接应用到Chip
。
这是什么原因?这是一个错误吗?
themes.xml
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Base application theme. -->
<style name="Theme.WriteWeight" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/light_green_dark</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. -->
<item name="android:textViewStyle">@style/TextViewFont</item>
<item name="android:buttonStyle">@style/ButtonStyle</item>
<item name="chipStyle">@style/ChipStyle</item>
</style>
<style name="TextViewFont" parent="@android:style/Widget.DeviceDefault.TextView">
<item name="android:fontFamily">@font/roboto_font_family</item>
<item name="android:includeFontPadding">false</item>
</style>
<style name="ButtonStyle" parent="@android:style/Widget.DeviceDefault.Button">
<item name="android:fontFamily">@font/roboto_regular</item>
<item name="android:includeFontPadding">false</item>
<item name="backgroundTint">@color/light_green</item>
</style>
<style name="ChipStyle" parent="Widget.MaterialComponents.Chip.Choice">
<item name="android:fontFamily">@font/roboto_regular</item>
<item name="android:includeFontPadding">false</item>
</style>
</resources>
roboto_font_family.xml
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- thin -->
<font
android:font="@font/roboto_thin"
android:fontStyle="normal"
android:fontWeight="100"
app:font="@font/roboto_thin"
app:fontStyle="normal"
app:fontWeight="100"/>
<!-- light -->
<font
android:font="@font/roboto_light"
android:fontStyle="normal"
android:fontWeight="300"
app:font="@font/roboto_light"
app:fontStyle="normal"
app:fontWeight="300"/>
<!-- regular -->
<font
android:font="@font/roboto_regular"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/roboto_black"
app:fontStyle="italic"
app:fontWeight="400" />
</font-family>
fragment_body_part_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:backgroundTint="@color/light_green">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border_dialog">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcy_part"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
tools:listitem="@layout/body_part_item"/>
</FrameLayout>
<Button
android:id="@+id/check"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="start"
android:textSize="27dp"
android:backgroundTint="@color/light_green"/>
</LinearLayout>
fragment_workout_list_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<HorizontalScrollView
android:id="@+id/horizontal_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/routine_list_recycler"
android:scrollbars="none"
android:overScrollMode="never">
<com.google.android.material.chip.ChipGroup
android:id="@+id/routine_chipgroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
app:chipSpacingHorizontal="10dp"
app:singleLine="true"
app:singleSelection="true"
style="@style/ChipStyle">
<com.google.android.material.chip.Chip
android:id="@+id/chip_back"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"
android:text="BACK"
app:rippleColor="@color/light_green"
style="@style/CustomChipChoice"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_chest"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"
android:text="CHEST"
app:rippleColor="@color/light_green"
style="@style/CustomChipChoice"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_leg"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"
android:text="LEG"
app:rippleColor="@color/light_green"
style="@style/CustomChipChoice"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_sholuder"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"
android:text="SHOULDER"
app:rippleColor="@color/light_green"
style="@style/CustomChipChoice"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_biceps"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"
android:text="BICEPS"
app:rippleColor="@color/light_green"
style="@style/CustomChipChoice"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_triceps"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"
android:text="TRICEP"
app:rippleColor="@color/light_green"
style="@style/CustomChipChoice"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip_abs"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center_horizontal"
android:text="ABS"
app:rippleColor="@color/light_green"
style="@style/CustomChipChoice"/>
</com.google.android.material.chip.ChipGroup>
</HorizontalScrollView>
<View
android:id="@+id/horizontal_bar"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/light_green"
app:layout_constraintTop_toBottomOf="@id/horizontal_scroll"
app:layout_constraintBottom_toTopOf="@id/routine_list_recycler"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/routine_list_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/horizontal_bar" />
</androidx.constraintlayout.widget.ConstraintLayout>
您可以在 Material 主题中设置按钮样式:
<item name="materialButtonStyle">@style/ButtonStyle</item>
<style name="ButtonStyle" parent="Widget.MaterialComponents.Button">
...