在我的Android应用程序中,我希望标准/基本标题栏改变颜色.
要更改您拥有的文本颜色setTitleColor(int color)
,有没有办法更改条形图的背景颜色?
有很多教程和SO的问题,实现自定义标题栏.但是,在我的自定义标题栏中,我有一个自定义渐变背景,我想知道如何在我的代码中动态设置它.
这是我的自定义标题栏被调用的地方:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.foo_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
Run Code Online (Sandbox Code Playgroud)
这是我的custom_title_bar
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@layout/custom_title_bar_background_colors">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/title_bar_logo"
android:gravity="center_horizontal"
android:paddingTop="0dip"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
如您所见,线性布局的背景由此人定义:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#616261"
android:endColor="#131313"
android:angle="270"
/>
<corners android:radius="0dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
我想要做的是在我的代码中动态设置这些渐变颜色.我不想像我们现在那样在我的XML文件中对它们进行硬编码.
如果您有更好的方法来设置背景渐变,我会对所有想法持开放态度.
先感谢您!!
我想尝试这种有趣的标题栏着色,但它对我不起作用,因为
getWindow().findViewById(android.R.id.title);
Run Code Online (Sandbox Code Playgroud)
返回空值。所以我用 Hierarchy Viewer 查看了它,发现调用了视图id/action_bar
。但没有R.id.action_bar
(自动完成不提供它,也没有这样的东西R.java
)。
所以现在我双重困惑:
android.R.id.title
(我在模拟器中使用版本 16)?id/action_bar
?我应该使用 ActionBarSherlock 吗?我本来只是想改变标题栏的颜色......而不是胡闹很多。