【问题标题】:Can I make the color of a toolbar a gradient?我可以将工具栏的颜色设为渐变吗?
【发布时间】:2016-06-27 14:54:33
【问题描述】:

在 android 中,我有一个主要的浅色和状态栏,我可以在 #F3A183#EC6F66 之间制作渐变吗?如果是这样,我该如何实现?

【问题讨论】:

    标签: android android-layout material-design


    【解决方案1】:
    <android.support.v7.widget.Toolbar
     android:id="@id/toolbar"
     app:theme="@style/my_toolbar"
     android:background="@drawable/ab_gradient"
     android:elevation="5dp"
     android:layout_gravity="top"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"/>
    

    ab_gradient.xml

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">
    
      <gradient
       android:type="linear"
       android:startColor="#F3A183"
       android:endColor="#EC6F66"
       android:angle="270"/>
    </shape>
    

    【讨论】:

      【解决方案2】:

      你可以像这样设置工具栏的背景

      toolbar.setBackgroundResource(R.drawable.gradient_one);
      

      您的渐变可能如下所示

      <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
      >
        <corners android:radius="9dp"/>
      
        <gradient
          android:startColor="#cc4151"
          android:centerColor="#d95a5e"
          android:endColor="#e27e70">
        </gradient>
      </shape>
      

      【讨论】:

        【解决方案3】:

        您还可以通过 ActionBar 以编程方式设置渐变颜色

        ActionBar actionBar = getSupportActionBar();
        actionBar.setBackgroundDrawable(AppCompatResources.getDrawable(context, R.drawable.toolbar_background));
        

        toolbar_background 可绘制渐变颜色如下:

        <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
            <gradient
                android:angle:="45"
                android:startColor="#F3A183"
                android:endColor="#EC6F66">
            </gradient>
            <size android:width="24dp"/>
            <size android:height="24dp"/>
        </shape>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-26
          相关资源
          最近更新 更多