Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 设置背景操作栏的渐变_Android_Xml_Android Actionbar_Gradient - Fatal编程技术网

Android 设置背景操作栏的渐变

Android 设置背景操作栏的渐变,android,xml,android-actionbar,gradient,Android,Xml,Android Actionbar,Gradient,我尝试为背景操作栏设置渐变,但背景与父操作栏不匹配 下面是代码梯度和操作栏 在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:useLeve

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

下面是代码梯度和操作栏


在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>
有关v7库的支持(您的案例):


在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>
有关v7库的支持(您的案例):

public class MainActivity extends AppCompatActivity {
DrawerLayout drawer;
private TabLayout tabLayout;
private ViewPager viewPager;
Toolbar toolbar;
TextView tvName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    tvName = (TextView) toolbar.findViewById(R.id.toolbar_title);
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    viewPager = (ViewPager)findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    tabLayout = (TabLayout)findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.actionbar_layout);
}
<?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>
ActionBar actionBar = getActionBar();    
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.mygradient));
// make sure to import android.support.v7.app.ActionBar
ActionBar actionBar = getSupportActionBar();        
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.mygradient));