Android LinearLayout グラデーション背景 質問する

Android LinearLayout グラデーション背景 質問する

LinearLayout にグラデーション背景を適用する際に問題が発生しています。

これは私が読んだところによると比較的簡単なはずですが、うまく動作しないようです。参考までに、私は 2.1-update1 で開発しています。

ヘッダー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>

メインヘッダー.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>

@drawable/header_bg を色 (例: #FF0000) に変更すると、問題なく動作します。ここで何か明らかなことを見逃しているのでしょうか?

ベストアンサー1

セレクターを使用してこの問題を解決することができました。以下のコードを参照してください。

メインヘッダー.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>

メインヘッダーセレクター.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>

これが同じ問題を抱えている誰かの助けになれば幸いです。

おすすめ記事