为什么我的按钮不显示设置样式?
Why does my button does not display set style?
我尝试更改按钮的样式,但它根本不显示设置的样式。它仅显示 'default' 矩形样式。我阅读了 "Style and Themes" 官方 android 文档,问题与样式层次无关。这是我的布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
style="@android:style/Widget.Button.Toggle"
android:layout_width="125dp"
android:layout_height="155dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@android:color/holo_red_light"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.74" />
</android.support.constraint.ConstraintLayout>
和我的styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
我还在 onCreate() 函数中使用上面的布局文件设置了内容视图。好像找不到@android:style 资源?请帮助
我也运行清理了项目并得到了
W/ResourceType(15056): For resource 0x0101053d, entry index(1341) is beyond
type entryCount(1320)
W/ResourceType(15056): For resource 0x0101053e, entry index(1342) is beyond
type entryCount(1320)
W/ResourceType(15056): For resource 0x0101053b, entry index(1339) is beyond
type entryCount(1320)
W/ResourceType(15056): For resource 0x0101053c, entry index(1340) is beyond
type entryCount(1320)
在res/values/styles.xml中需要定义一个按钮样式如'MyButtonStyle'
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="MyButtonStyle" parent="Widget.AppCompat.Button">
<item name="android:background">@drawable/button</item>
</style>
在 res/drawable/button.xml 中,您定义按钮的基线样式,该样式被定义为引用按下和默认状态的选择器,例如。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/button_pressed"/>
<item android:drawable="@drawable/button_normal"/>
</selector>
在res/drawable/button_正常
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1dp"
android:color="@color/buttonDarkShade"/>
<gradient
android:type="linear"
android:angle="90"
android:startColor="@color/buttonDarkShade"
android:centerColor="@color/buttonHighlightShade"
android:endColor="@color/buttonDarkShade"
/>
</shape>
在res/drawable/button_pressed
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="4dp"
android:color="@color/colorPrimaryDark"/>
<gradient
android:type="linear"
android:angle="90"
android:startColor="@color/colorPrimary"
android:centerColor="@color/buttonHighlightShade"
android:endColor="@color/colorPrimary"
/>
<corners android:radius="16dp" />
</shape>
这是制作自定义按钮的基本模板。定义上面使用的 res/values/colors 并根据需要更改或添加选择器和形状属性。
当然,当你在布局中使用按钮样式时,记得设置样式
<Button
android:id="@+id/myCustomStylishButton"
style="@style/MyButtonStyle"/>
我发现 source code 包含样式 "Widget.Button.Toggle"。这似乎是一种旧风格,我想它不可能与AppCompat应用程序主题一起使用。
我这么认为的一个原因是因为链接的回购在其路径中有名称 "gingerbread",另一个原因是我的 Android Studio (3.3) 不知道样式 - 如果我尝试使用它,文本变成红色。
但是你说你不能成功应用任何样式。所以我将您的代码片段复制到我的示例应用程序中并执行了以下操作:
步骤 1 在 res/values/styles.xml 中声明一个样式并尝试将其与 Button
:
<style name="MyButtonStyle">
<item name="android:textColor">#00ff00</item>
</style>
在activity_main.xml
<Button
android:id="@+id/button"
style="@style/MyButtonStyle"
android:layout_width="125dp"
android:layout_height="155dp"
android:background="@android:color/holo_red_light"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.74"
android:text="Hello World"/>
结果:
步骤 2 将已知的 AppCompat 样式与 Button
一起使用,例如 Widget.AppCompat.Button.Borderless.Colored
。 (我保留背景只是为了表明它有效,即使带有背景的无边界 Button
听起来有点自相矛盾)
<Button
android:id="@+id/button"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="125dp"
android:layout_height="155dp"
android:background="@android:color/holo_red_light"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.74"
android:text="Hello World"/>
结果:
步骤 3(甚至比其他两个更可选)选择一些 AppCompat Button
样式,尝试使其工作并返回这里,如果你 运行 出错。
(一般建议:如有必要,请将 Android Studio 更新为当前稳定版本,并确保不要在模块 [= 的依赖闭包中混用 AndroidX 库和支持库45=]build.gradle 文件)
感谢大家的回答,但我发现我正在寻找的是将 ImageButton 小部件添加到我的 xml sheet 中,而不是 Button 小部件
我尝试更改按钮的样式,但它根本不显示设置的样式。它仅显示 'default' 矩形样式。我阅读了 "Style and Themes" 官方 android 文档,问题与样式层次无关。这是我的布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
style="@android:style/Widget.Button.Toggle"
android:layout_width="125dp"
android:layout_height="155dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@android:color/holo_red_light"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.74" />
</android.support.constraint.ConstraintLayout>
和我的styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
我还在 onCreate() 函数中使用上面的布局文件设置了内容视图。好像找不到@android:style 资源?请帮助
我也运行清理了项目并得到了
W/ResourceType(15056): For resource 0x0101053d, entry index(1341) is beyond
type entryCount(1320)
W/ResourceType(15056): For resource 0x0101053e, entry index(1342) is beyond
type entryCount(1320)
W/ResourceType(15056): For resource 0x0101053b, entry index(1339) is beyond
type entryCount(1320)
W/ResourceType(15056): For resource 0x0101053c, entry index(1340) is beyond
type entryCount(1320)
在res/values/styles.xml中需要定义一个按钮样式如'MyButtonStyle'
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="MyButtonStyle" parent="Widget.AppCompat.Button">
<item name="android:background">@drawable/button</item>
</style>
在 res/drawable/button.xml 中,您定义按钮的基线样式,该样式被定义为引用按下和默认状态的选择器,例如。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/button_pressed"/>
<item android:drawable="@drawable/button_normal"/>
</selector>
在res/drawable/button_正常
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1dp"
android:color="@color/buttonDarkShade"/>
<gradient
android:type="linear"
android:angle="90"
android:startColor="@color/buttonDarkShade"
android:centerColor="@color/buttonHighlightShade"
android:endColor="@color/buttonDarkShade"
/>
</shape>
在res/drawable/button_pressed
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="4dp"
android:color="@color/colorPrimaryDark"/>
<gradient
android:type="linear"
android:angle="90"
android:startColor="@color/colorPrimary"
android:centerColor="@color/buttonHighlightShade"
android:endColor="@color/colorPrimary"
/>
<corners android:radius="16dp" />
</shape>
这是制作自定义按钮的基本模板。定义上面使用的 res/values/colors 并根据需要更改或添加选择器和形状属性。
当然,当你在布局中使用按钮样式时,记得设置样式
<Button
android:id="@+id/myCustomStylishButton"
style="@style/MyButtonStyle"/>
我发现 source code 包含样式 "Widget.Button.Toggle"。这似乎是一种旧风格,我想它不可能与AppCompat应用程序主题一起使用。
我这么认为的一个原因是因为链接的回购在其路径中有名称 "gingerbread",另一个原因是我的 Android Studio (3.3) 不知道样式 - 如果我尝试使用它,文本变成红色。
但是你说你不能成功应用任何样式。所以我将您的代码片段复制到我的示例应用程序中并执行了以下操作:
步骤 1 在 res/values/styles.xml 中声明一个样式并尝试将其与 Button
:
<style name="MyButtonStyle">
<item name="android:textColor">#00ff00</item>
</style>
在activity_main.xml
<Button
android:id="@+id/button"
style="@style/MyButtonStyle"
android:layout_width="125dp"
android:layout_height="155dp"
android:background="@android:color/holo_red_light"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.74"
android:text="Hello World"/>
结果:
步骤 2 将已知的 AppCompat 样式与 Button
一起使用,例如 Widget.AppCompat.Button.Borderless.Colored
。 (我保留背景只是为了表明它有效,即使带有背景的无边界 Button
听起来有点自相矛盾)
<Button
android:id="@+id/button"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="125dp"
android:layout_height="155dp"
android:background="@android:color/holo_red_light"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.74"
android:text="Hello World"/>
结果:
步骤 3(甚至比其他两个更可选)选择一些 AppCompat Button
样式,尝试使其工作并返回这里,如果你 运行 出错。
(一般建议:如有必要,请将 Android Studio 更新为当前稳定版本,并确保不要在模块 [= 的依赖闭包中混用 AndroidX 库和支持库45=]build.gradle 文件)
感谢大家的回答,但我发现我正在寻找的是将 ImageButton 小部件添加到我的 xml sheet 中,而不是 Button 小部件