Применение градиентного фона на панели инструментов - PullRequest
0 голосов
/ 20 июня 2019

Я создал пользовательский градиентный файл для рисования, который я хочу использовать в качестве фона Toolbar, но не могу его применить.Он просто отображается как серый (см. Изображение).Я делаю что-то неправильно?Я просмотрел несколько сообщений, но пока у меня ничего не получалось.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <Toolbar
        android:id="@+id/home_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/home_toolbar_gradient"
        android:elevation="4dp"
        android:minHeight="70dp"
        android:theme="@style/customToolbar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <TextView
            android:id="@+id/title_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:fontFamily="@font/Montserrat-Bold"
            android:gravity="center"
            android:text="@string/home"
            android:textAlignment="center"
            android:textColor="@android:color/white" />
    </Toolbar>

    <!-- Using a view with a gradient to create a drop navbar_shadow effect for the navbar -->
    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_above="@id/bottom_nav_bar"
        android:background="@drawable/navbar_shadow" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        app:labelVisibilityMode="labeled"
        android:id="@+id/bottom_nav_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@android:color/white"
        app:itemIconTint="@drawable/nav_selected_item_color"
        app:itemTextColor="@drawable/nav_selected_item_color"
        app:menu="@menu/bottom_nav_bar">


    </com.google.android.material.bottomnavigation.BottomNavigationView>

</RelativeLayout>

home_toolbar_gradient.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:type="linear"
        android:startColor="@color/toolbar_starting_color"
        android:endColor="@color/toolbar_ending_color"
        android:angle="75" />
</shape>

styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:fontFamily">@font/montserrat_medium</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <!-- Custom Rounded Dialog style -->
    <style name="RoundedDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:windowBackground">@drawable/rounded_corners</item>
    </style>

    <style name="customToolbar" parent="Theme.AppCompat.Light.NoActionBar" />

</resources>

в MainActivity.java:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar homeToolbar = findViewById(R.id.home_toolbar);

setSupportActionBar(homeToolbar);

и в манифесте:

android:theme="@style/Theme.AppCompat.Light.DarkActionBar"

toolbar result

Ответы [ 3 ]

1 голос
/ 21 июня 2019

Для тех, кто заинтересован, я действительно нашел проблему. Атрибут angle в gradient должен быть кратен 45, иначе вы получите небольшую ошибку рендеринга в предварительном просмотре макета. Просто измените его на кратное, которое соответствует вашим потребностям, и градиент вступит в силу.

0 голосов
/ 20 июня 2019

Попробуйте это:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:centerColor="#555994"
        android:endColor="#b5b6d2"
        android:startColor="#555994"
        android:type="linear" />

    <corners 
        android:radius="0dp"/>

</shape>
0 голосов
/ 20 июня 2019

Попробуйте сделать это динамически:

setSupportActionBar(homeToolbar);
ActionBar actionbar = getSupportActionBar();
actionbar.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_toolbar_gradient));

Читать это .Это говорит- Угол градиента, используемый только с линейным градиентом.Должно быть кратно 45 в диапазоне [0, 315].

Итак, измените home_toolbar_gradient.xml на

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  <gradient
    android:type="linear"
    android:startColor="#FF33"
    android:endColor="#AFF"
    android:angle="45" />
</shape>
...