Android Material Стиль кнопки не меняется - PullRequest
0 голосов
/ 11 декабря 2018

Я пытаюсь стилизовать кнопки материала, используя новые стили, предлагаемые библиотекой 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 голосов
/ 11 декабря 2018

Стили, которые вы опубликовали, не имеют смысла.

  • Вы показываете стили ToolBarStyle и WhiteButtons, но нигде их не используете.
  • У вас есть MyMaterialTheme (в отличие от MyMaterialTheme.Base) в одном из файлов;Я надеюсь, что вы не ожидаете, что это будет автоматически унаследовано MyMaterialTheme.Base, поскольку указание parent переопределяет наследование точечной нотации.
  • Все три файла имеют одинаковое содержимое для MyMaterialTheme.Base;зачем указывать его в трех местах?

Кроме того, ни один из опубликованных вами стилей, похоже, не повлияет на виджеты MaterialButton (кроме colorPrimary).Поэтому трудно точно определить, в чем заключается ваша проблема.

Все вышесказанное, список атрибутов, поддерживаемых MaterialButton, доступен в документации GitHub .Примечательно, что android:background - это , а не , поддерживаемый MaterialButton JavaDoc :

Не используйте атрибут android:background.MaterialButton управляет собственным фоном для рисования, а установка нового фона означает, что MaterialButton больше не может гарантировать, что новые атрибуты, которые он вводит, будут работать правильно.Если фон по умолчанию изменен, MaterialButton не может гарантировать четкое поведение.

Я подозреваю, что вы видите комбинацию различных определений colorPrimary на двух экранах и фактаandroid:background не поддерживается.

Если вы хотите вручную переопределить цвет фона кнопки, используйте атрибут app:backgroundTint в теге <MaterialButton>.Если вы хотите указать это в теге <style>, он не использует пространство имен:

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