设置背景操作栏的渐变

时间:2018-05-27 10:20:28

标签: android xml android-actionbar gradient

我尝试为背景操作栏设置渐变,但背景与父操作栏不匹配

这是代码gradient_actionbar

 <node>
 <!--1. copy everything-->
<CTO>
    <id>TRFG2</id>
    <order_number>TRFG2</order_number>
    <!--2. exclude address tag here-->
    <PT>
        <reference/>
        <comment/>
    </PT>
    <DT>
        <address>
            <id>C1050692</id>
            <code>C1050692</code>
        </address>
        <comment>This is a comment.</comment>
    </DT>
    <OLS>
        <OL>
            <!--3. match OL ID-->
            <id>TS1</id>
            <PT>
                <!--4. and add here the value from the outputquery result-->
                <addressId>VAL1</addressId>
            </PT>
            <DT>
                <station>
                    <id>C1050692-01</id>
                    <code>C1050692-01</code>
                    <addressId>C1050692</addressId>
                </station>
            </DT>
        </OL>
        <OL>
            <id>TS2</id>
            <PT>
                <addressId>VAL2</addressId>
            </PT>
            <DT>
                <station>
                    <id>C1050692-01</id>
                    <code>C1050692-01</code>
                    <addressId>C1050692</addressId>
                </station>
            </DT>
        </OL>
    </OLS>
</CTO>
</node>

这是代码actionbar_layout

<gradient
    android:angle="360"
    android:centerColor="@color/color2"
    android:endColor="@color/color3"
    android:startColor="@color/color1"
    android:type="linear"
     />

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

这是代码MainActivity.java

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient_actionbar"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Test"
        android:textColor="#ffffff"
        android:id="@+id/mytext"
        android:textSize="18sp" />

</LinearLayout>

This layout when run application

1 个答案:

答案 0 :(得分:0)

在res / drawable / mygradient.xml中创建一个GradientDrawable

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

    <gradient
        android:angle="360"
        android:centerColor="@color/color2"
        android:endColor="@color/color3"
        android:startColor="@color/color1"
        android:type="linear" />

</shape>

然后在活动的onCreate()中将其设置为操作栏:

ActionBar actionBar = getActionBar();    
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.mygradient));

对于支持v7库(您的情况):

// make sure to import android.support.v7.app.ActionBar
ActionBar actionBar = getSupportActionBar();        
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.mygradient));