如何以编程方式设置一个自定义标题栏的背景颜色渐变? [英] How do I programmatically set the background color gradient on a Custom Title Bar?
问题描述
有很多教程在那里和问题上,以便实现自定义标题栏。然而,在我的自定义标题栏我有一个自定义渐变为背景,我想知道如何动态地在我的code设置。
There are many tutorials out there and questions on SO that implement custom title bars. However, in my custom title bar I have a custom gradient for the background and I would like to know how to set it dynamically in my code.
下面是我的自定义标题栏被调用:
Here is where my custom title bar gets called:
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>
正如你所看到的,对线性布局的背景是这家伙定义的:
As you can see, the background on the linear layout is defined by this guy:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#616261"
android:endColor="#131313"
android:angle="270"
/>
<corners android:radius="0dp" />
我想要做的是动态设置的渐变颜色在我的code。我不希望到c他们难以$ C $在我的XML文件,就像他们现在是。
What I would like to do is set those gradient colors dynamically in my code. I do not want to hard code them in my XML file like they currently are.
我向所有人开放的想法,如果你有设置背景渐变的更好的方法。
I am open to all ideas if you have a better way of setting a background gradient.
感谢你在前进!
推荐答案
要做到这一点在code,创建GradientDrawable。
唯一的机会来设置角度和色彩是在构造函数中。
如果你想改变颜色或角度,只要创建一个新GradientDrawable,并将其设置为背景
To do this in code, you create a GradientDrawable.
The only chance to set the angle and color is in the constructor.
If you want to change the color or angle, just create a new GradientDrawable and set it as the background
View layout = findViewById(R.id.mainlayout);
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);
layout.setBackgroundDrawable(gd);
对于这项工作,我添加了一个ID为你的主要的LinearLayout如下:
For this to work, I added an id to your main LinearLayout as follows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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>
和使用这个作为一个自定义标题栏
And to use this as for a custom title bar
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title_bar);
View title = getWindow().findViewById(R.id.mainlayout);
title.setBackgroundDrawable(gd);
这篇关于如何以编程方式设置一个自定义标题栏的背景颜色渐变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!