Android MaterialButton样式未更改

时间:2018-12-10 23:23:29

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

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

此外,我通过设置style="myButtonStyleWhatever"来手动覆盖一组按钮的样式,但这也被忽略了。这变得非常令人沮丧,所以我希望有人能帮助您。

Styles-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>

Styles-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" />

不应该看起来像这样

enter image description here

<!-- 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>

1 个答案:

答案 0 :(得分:1)

您发布的样式没有意义。

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

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

总而言之,the GitHub documentation中提供了MaterialButton支持的属性列表。值得注意的是,android:background不支持MaterialButton。来自the JavaDoc

  

请勿使用android:background属性。 MaterialButton管理其自己的背景可绘制对象,设置新的背景意味着MaterialButton不能再保证其引入的新属性将正常运行。如果更改了默认背景,MaterialButton将无法保证行为明确。

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

如果要手动覆盖按钮的背景色,请在app:backgroundTint标签上使用<MaterialButton>属性。如果要在<style>标记中指定该标记,则不使用名称空间:

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