1

我正在尝试使用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>
4

1 回答 1

4

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

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

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

话虽如此, GitHub 文档MaterialButton中提供了支持的属性列表。值得注意的是,支持. 从JavaDocandroid:backgroundMaterialButton

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

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

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

<item name="backgroundTint">@color/your_background_color</item>
于 2018-12-10T23:46:27.317 回答