【问题标题】:Android MaterialButton style not changingAndroid MaterialButton 样式不变
【发布时间】:2018-12-10 23:23:28
【问题描述】:

我正在尝试使用implementation 'com.google.android.material:material:1.0.0' 库提供的新样式设置材质按钮的样式。问题是按钮的样式不正确。例如,我有 2 个不同的活动,使用完全相同的主题,按钮代码完全相同,但拒绝使用正确的样式,在 Android 预览屏幕上它看起来正确,但在实际应用中却不是。

另外,我通过设置style="myButtonStyleWhatever" 手动覆盖了一组按钮的样式,但这也被忽略了。这变得非常令人沮丧,所以如果有人可以提供帮助,我会很高兴。

样式-v11

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyMaterialTheme.Base" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorPrimaryDark</item>
    </style>

    <style name="ToolBarStyle" parent="Theme.AppCompat">
        <item name="colorPrimary">#364c70</item>
        <item name="actionMenuTextColor">@android:color/white</item>
    </style>

    <style name="WhiteButtons" parent="Widget.MaterialComponents.Button">
        <item name="android:background">#ffff</item>
        <item name="android:textColor">#000</item>
    </style>

</resources>

样式-v21

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyMaterialTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowAllowEnterTransitionOverlap">true</item>
        <item name="android:windowAllowReturnTransitionOverlap">true</item>
        <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
        <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
    </style>

    <style name="MyMaterialTheme.Base" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorPrimaryDark</item>
    </style>

</resources>

样式

<resources>

    <style name="MyMaterialTheme.Base" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorPrimaryDark</item>
    </style>

    <style name="ToolBarStyle" parent="Theme.AppCompat">
        <item name="colorPrimary">#364c70</item>
        <item name="actionMenuTextColor">@android:color/white</item>
    </style>

    <style name="WhiteButtons" parent="Widget.MaterialComponents.Button">
        <item name="android:background">#ffff</item>
        <item name="android:textColor">#000</item>
    </style>

</resources>

用于按钮的代码

<com.google.android.material.button.MaterialButton
            android:id="@+id/get_suggestions"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_width="@dimen/_65sdp"
            android:layout_height="wrap_content"
            android:layout_marginStart="163dp"
            android:layout_marginTop="83dp"
            android:layout_marginEnd="163dp"
            android:layout_marginBottom="219dp"
            android:text="SEARCH"
            android:textSize="@dimen/_9sdp"
            app:cornerRadius="100dp" />

不应该是那样的

<!-- MAIN CONTENT -->
<com.sothree.slidinguppanel.SlidingUpPanelLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sothree="http://schemas.android.com/apk/res-auto"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/xmlns:app="
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:windowSoftInputMode="adjustNothing"
sothree:umanoPanelHeight="68dp"
sothree:umanoShadowHeight="4dp"
sothree:umanoParallaxOffset="100dp"
sothree:umanoDragView="@+id/dragger"
sothree:umanoOverlay="true"
sothree:umanoScrollableView="@+id/dragView">

<androidx.drawerlayout.widget.DrawerLayout
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:focusableInTouchMode="true"
    android:layout_height="match_parent">


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:id="@+id/parent"
            android:layout_height="match_parent">




            <com.mancj.materialsearchbar.MaterialSearchBar
                android:id="@+id/search"
                android:layout_width="@dimen/_233sdp"
                android:layout_height="@dimen/_58sdp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="5dp"
                android:elevation="100dp"
                android:imeOptions="actionSearch"
                sothree:layout_constraintStart_toStartOf="parent"
                sothree:layout_constraintTop_toTopOf="parent"
                tools:ignore="UnusedAttribute" />

            <com.google.android.material.button.MaterialButton

style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                android:id="@+id/get_suggestions"
                android:layout_width="@dimen/_65sdp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="4dp"
                android:layout_marginTop="10dp"
                app:cornerRadius="100dp"
                android:text="SEARCH"
                android:textSize="@dimen/_9sdp"
                sothree:layout_constraintEnd_toEndOf="parent"
                sothree:layout_constraintTop_toTopOf="parent" />



            <com.mapbox.mapboxsdk.maps.MapView
                android:id="@+id/mapview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                mapbox:mapbox_cameraTargetLat="46.885"
                mapbox:mapbox_cameraTargetLng="24.679"
                mapbox:mapbox_cameraTilt="20"
                mapbox:mapbox_cameraZoom="4"

mapbox:mapbox_styleUrl="mapbox://styles/sudafly/cjm89d4011nu02smk5a7e0d8h"
                sothree:layout_constraintBottom_toBottomOf="parent"
                sothree:layout_constraintEnd_toEndOf="parent"
                sothree:layout_constraintStart_toStartOf="parent"
                sothree:layout_constraintTop_toTopOf="parent">

            </com.mapbox.mapboxsdk.maps.MapView>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </FrameLayout>

     <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/nav_menu"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/navigation_menu"
        />

  </androidx.drawerlayout.widget.DrawerLayout>






  <!-- SLIDING LAYOUT -->
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/dragView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:clickable="true"
    android:focusable="false"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="68dp"
        android:background="#2a2e41"
        android:id="@+id/dragger"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/name"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:text="@string/search_for_airports"
            android:textColor="#d2d4e0"
            android:textSize="20dp" />


    </LinearLayout>

    <androidx.constraintlayout.widget.ConstraintLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/drag"
        android:background="#eceff1">


        <LinearLayout
            android:id="@+id/Lin"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#eceff1"
                android:elevation="@dimen/_20sdp"
                android:theme="@style/AppTheme.AppBarOverlay"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tab"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                </com.google.android.material.tabs.TabLayout>

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:padding="0dp">


                    <include layout="@layout/charts_v2" />

                    <include layout="@layout/content_main" />


                </androidx.constraintlayout.widget.ConstraintLayout>


            </com.google.android.material.appbar.AppBarLayout>

        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>


 </LinearLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>

【问题讨论】:

    标签: android android-studio material-design material-theme


    【解决方案1】:

    您发布的样式没有多大意义。

    • 您正在展示 ToolBarStyleWhiteButtons 样式,但您没有在任何地方使用它们。
    • 您在其中一个文件中有MyMaterialTheme(而不是MyMaterialTheme.Base);我希望您不要期望 MyMaterialTheme.Base 自动继承它,因为指定 parent 会覆盖点符号继承。
    • MyMaterialTheme.Base 的三个文件的内容相同;为什么还要在三个地方指定它?

    此外,您发布的所有样式似乎都不会影响MaterialButton 小部件(colorPrimary 除外)。所以很难确切地知道你的问题出在哪里。

    话虽如此,MaterialButton 支持的属性列表可在the GitHub documentation 中找到。值得注意的是,android:backgroundMaterialButton 支持。来自the JavaDoc

    不要使用android:background 属性。 MaterialButton 管理自己的背景可绘制对象,设置新背景意味着MaterialButton 不能再保证它引入的新属性能够正常工作。如果更改默认背景,MaterialButton 无法保证良好定义的行为。

    我怀疑您看到的是两个屏幕上不同的colorPrimary 定义的组合以及不支持android:background 的事实。

    如果您想手动覆盖按钮的背景颜色,请在您的&lt;MaterialButton&gt; 标记上使用app:backgroundTint 属性。如果你想在 &lt;style&gt; 标签中指定它,它不使用命名空间:

    <item name="backgroundTint">@color/your_background_color</item>
    

    【讨论】:

    • 谢谢,我会做一些改变,但我正在为我的一个按钮使用“style="@style/Widget.MaterialComponents.Button.OutlinedButton",它实际上看起来并不像它的意思
    • 在android预览中它使用“outline”样式,但在设备上它看起来像在图片中
    • 您发布的代码中没有任何内容可以解释为什么OutlinedButton 样式不起作用。相关信息是您的按钮所在的 Activity 的 AndroidManifest.xml &lt;activity&gt; 条目、您的 Activity 正在使用的主题的 &lt;style&gt; 定义以及您的按钮包含在其中的完整布局 xml(以便我们可以查看是否有任何父视图可能会影响它)以及布局中&lt;MaterialButton&gt; 标记的任何父级使用的任何&lt;style&gt; 定义。
    • 您正在测试的设备/模拟器也很重要,尤其是 Android 操作系统版本。
    • P &lt;activity android:name=".DemoActivity" android:windowSoftInputMode="adjustPan"&gt; &lt;/activity&gt; 参加活动。我正在 Pixel 2 XL Android P 上对其进行测试。我更新了帖子并为布局添加了整个 XML。我不太明白您对主题的
    猜你喜欢
    • 2019-10-09
    • 2019-07-27
    • 1970-01-01
    • 2021-05-16
    • 2020-06-17
    • 2019-02-23
    • 1970-01-01
    • 2020-06-20
    • 2016-10-24
    相关资源
    最近更新 更多