为什么我的按钮不会显示设置的风格?

问题描述 投票:1回答:3

我试图改变我的按钮的风格,但它根本不显示设置的风格。它只是显示了“默认”矩形造型。我读“样式和主题”官方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:风格的ressource找不到?请帮忙

我也跑干净的项目,并得到了

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)
android android-layout android-styles
3个回答
0
投票

在RES /价值/ 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 /绘制/ 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 /绘制/ button_normal

<?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 /绘制/ 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>

这是为了使自定义按钮的基本模板。定义上面使用的RE /值/颜色和更改或添加选择器并根据需要形状属性。

当然,当你在布局中使用的按钮样式记得设置样式

<Button
    android:id="@+id/myCustomStylishButton"
    style="@style/MyButtonStyle"/>

0
投票

我发现含有式“Widget.Button.Toggle”的source code。这似乎是一个旧的风格,我想这是不可能与程序兼容性应用的主题使用。

原因之一,我想是这样,因为链接回购其路径名称为“姜饼”,另一个原因是,我的Android工作室(3.3)不知道的风格 - 如果我尝试使用它的文本变成红色。

但是你说你无法成功申请任何风格任何责任。所以,我复制你的代码片段到我的示例应用程序,也做了以下内容:

第1步声明一个风格RES /价值/ 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"/>

结果:

enter image description here

步骤2用已知的程序兼容性风格与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"/>

结果:

enter image description here

步骤3(比其他两个更可选)选择一些程序兼容性Button风格,尽量做到工作,如果你遇到错误,回到这里。

(一般的建议:更新Android Studio中的当前稳定版本,如果必要,并确保你不会在模块的build.gradle文件中的依赖封闭混合AndroidX库和支持库)


0
投票

感谢所有的答案,但我发现,我一直在寻找的是对的ImageButton控件相比添加到我的XML表的按钮组件

© www.soinside.com 2019 - 2024. All rights reserved.