Android 带有AppCompat v7支持库的style.xml中的ActionBarStyle
我搜索了整个网络。尝试了数十种解决方案。我也遵循了Android的文档,但我无法满足我的需求我只想制作actionbar背景渐变。我制作了可绘制的xml文件: action\u bar.xmlAndroid 带有AppCompat v7支持库的style.xml中的ActionBarStyle,android,android-actionbar,android-appcompat,Android,Android Actionbar,Android Appcompat,我搜索了整个网络。尝试了数十种解决方案。我也遵循了Android的文档,但我无法满足我的需求我只想制作actionbar背景渐变。我制作了可绘制的xml文件: action\u bar.xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:angle="270
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="270"
android:startColor="@color/colorActionBarStart"
android:endColor="@color/colorActionBarEnd" />
</shape>
My style.xml:
<resources>
<!-- the theme applied to the application or activity -->
<style name="AppTheme"
parent="Theme.AppCompat.Light">
<item name="actionBarStyle">@style/MyActionBarStyle</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBarStyle"
parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">@drawable/action_bar</item>
</style>
</resources>
@样式/MyActionBarStyle
错误的
符合事实的
@可拉拔/动作杆
我在android上看到过很多次同样的事情。答案有很多,但都不管用。我要做的就是给actionbar做一个渐变背景。以前没有人用appcompat v7库这样做过吗?为什么这么难?
可能是因为我对安卓系统的开发还很陌生,所以我无法获得。如果是这种情况,请告诉我正确的观点
请帮帮我。
谢谢。就我个人而言,我不使用像
theme.AppCompat.Light
这样的主题提供的默认操作栏。相反,我将这样做:
- 使用主题,如
theme.AppCompat.Light.NoActionBar
- 在我的布局中包括一个
标记 - 调用
并传递我的工具栏setSupportActionBar()
actionBarStyle
主题项,并在onCreate()
中用Java进行了操作,从而获得了一个渐变效果:
如果您的目标是为应用程序中的每个屏幕执行此操作,您可以创建一个BaseActivity
类,并让所有其他活动扩展该类,而不是AppCompatActivity
:
public class BaseActivity extends AppCompatActivity {
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Drawable d = ContextCompat.getDrawable(this, R.drawable.action_bar);
getSupportActionBar().setBackgroundDrawable(d);
}
}
我没有按你的方式做。但是,如果我这样做,我将为活动使用一个没有操作栏的主题,然后使用协调器布局。在该布局中,您可以使用应用程序栏布局来承载自定义工具栏。通过这种方式,您可以根据需要制作动作栏like@AnkyAn我也试过了,我使用了工具栏样式,但它不起作用。我的目标是从styles.xml完成这项工作。你怎么能这么做?你能把你的style.xml发给我吗?我不想使用内联样式属性。谢谢。这符合我的要求,但我有个问题。从styles.xml中不存在这样做的方法吗?
public class BaseActivity extends AppCompatActivity {
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Drawable d = ContextCompat.getDrawable(this, R.drawable.action_bar);
getSupportActionBar().setBackgroundDrawable(d);
}
}