4

アクションバーの下部にある分割バーの色をプログラムで変更しようとしています。私の戦略は、この XMLに基づいて、プログラムで生成された矩形LayerDrawableを含むアクション バーの背景を設定することです。ShapeDrawable

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Bottom Line -->
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/action_bar_line_color" />
        </shape>
    </item>

    <!-- Color of your action bar -->
    <item android:bottom="2dip">
        <shape android:shape="rectangle">
            <solid android:color="@color/action_bar_color" />
        </shape>
    </item>
</layer-list>

しかし、私は障害にぶつかりました:プログラムandroid:bottomでプロパティを (のように) 適用する方法がわかりません。<item android:bottom="2dip">明らかandroid:bottomに item タグのプロパティであり、(私が思うに) プログラム的に同等のものはなく、ShapeDrawable適切に見えるメソッド/プロパティを見つけることができませんでした。

これまでのコード:

public LayerDrawable createABBackground(String color) {
    ShapeDrawable rect = new ShapeDrawable(new RectShape());
    rect.getPaint().setColor(Color.parseColor("#000000"));
    ShapeDrawable rect2 = new ShapeDrawable(new RectShape());
    rect2.getPaint().setColor(Color.parseColor(color));
    ShapeDrawable[] layers = {rect, rect2};
    LayerDrawable background = new LayerDrawable(layers);
    return background;
}

アイデア?代替ソリューションが重要な場合は、ActionBarSherlock を使用しています。

編集:

MH が示唆するように、setLayerInset は、私が望んでいたことを行いました。これを使用した関数の修正版は次のとおりです。

public LayerDrawable createABBackground(String color) {
    ShapeDrawable rect = new ShapeDrawable(new RectShape());
    rect.getPaint().setColor(Color.parseColor(color));
    ShapeDrawable rect2 = new ShapeDrawable(new RectShape());
    rect2.getPaint().setColor(Color.parseColor("#000000"));
    ShapeDrawable[] layers = {rect, rect2};
    LayerDrawable background = new LayerDrawable(layers);
    background.setLayerInset(0, 0, 3, 0, 0);
    background.setLayerInset(1, 0, 0, 0, 3);
    return background;
}
4

4 に答える 4

1

LayerDrawableのソース コードから判断すると、それを可能にするメソッドは非公開です。ただし、InsetDrawable を保持するLayerDrawableを使用してこれを実現できる場合があります。

于 2013-07-04T14:30:38.137 に答える
0

プログラムで色を変更している理由を詳しく説明できますか? 背景色も制御していない場合、これを行うのは少し無意味に思えます。背景色を制御している場合は、おそらくスタイルを使用している可能性があります。その場合、これを行う方法がいくつか考えられます。

サイモン

于 2013-06-28T20:06:02.837 に答える
0

これは私のために働いた:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+id/actionBarLineParent">
        <shape
            android:id="@+id/actionBarLine"
            android:shape="rectangle" >
            <solid android:color="@color/red_all_files" />
        </shape>
    </item>

    <item
        android:id="@+id/actionBarColourParent"
        android:bottom="2dip">
        <shape
            android:id="@+id/actionBarColour"
            android:shape="rectangle" >
            <solid android:color="@android:color/white" />
        </shape>
    </item>

</layer-list>

次に呼び出します:

final LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.drawable.actionbar_background);
ld.setDrawableByLayerId(R.id.actionBarLineParent, new ColorDrawable(Color.BLUE));
getActionBar().setBackgroundDrawable(ld);

android:id を設定していることを確認する必要があります

于 2014-09-13T11:35:05.603 に答える