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));