安卓的LinearLayout渐变背景 [英] Android LinearLayout Gradient Background
问题描述
我有一个应用渐变背景到的LinearLayout麻烦。
I am having trouble applying a gradient background to a LinearLayout.
这应该是从相对简单的东西我看过,但它只是似乎并没有工作。供参考的缘故,我开发了2.1-UPDATE1。
This should be relatively simple from what I have read but it just doesn't seem to work. For reference sakes I am developing on 2.1-update1.
header_bg.xml:
header_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:startColor="#FFFF0000"
android:endColor="#FF00FF00"
android:type="linear"/>
</shape>
main_header.xml:
main_header.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal"
android:background="@drawable/header_bg">
</LinearLayout>
如果我更改@可绘制/ header_bg为一种颜色 - 例如#FF0000它完美的罚款。我失去了一些东西明显在这里?
If I change @drawable/header_bg to a color - e.g. #FF0000 it works perfectly fine. Am I missing something obvious here?
推荐答案
好,我已成功用一个选择来解决这个问题。见下面code:
Ok I have managed to solve this using a selector. See code below:
main_header.xml:
main_header.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal"
android:background="@drawable/main_header_selector">
</LinearLayout>
main_header_selector.xml:
main_header_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#FFFF0000"
android:endColor="#FF00FF00"
android:type="linear" />
</shape>
</item>
</selector>
希望这有助于谁的人有同样的问题。
Hopefully this helps someone who has the same problem.
这篇关于安卓的LinearLayout渐变背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!