有很多教程和stackoverflow上的问题实现了自定义标题栏。然而,在我的自定义标题栏中,我有一个自定义渐变背景,我想知道如何在我的代码中动态设置它。
这里是我的自定义标题栏被调用的地方:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.foo_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
这是我的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>
你可以看到,线性布局的背景是由这个属性定义的:
<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>
我的目标是在代码中动态设置渐变颜色。我不想像现在这样在 XML 文件中硬编码它们。
如果你有更好的方法来设置背景渐变,我非常乐意听取所有建议。
提前感谢你!
setBackgroundDrawable()
方法已被弃用,建议改用setBackground()
来替代或同时使用。详情请参考https://dev59.com/Ll4d5IYBdhLWcg3wJflm 。 - ban-geoengineering