30

線形レイアウトの背景としてドローアブルリソースを設定する小さなAndroidアプリケーションを開発しています。次に、線形レイアウトの背景色を動的に変更しますが、描画可能なリソース内で変更します。私のコードは次のようになります:

//  bcd.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item>        
    <shape>
        <gradient
            android:endColor="#22000000"
            android:startColor="#22000000"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="@color/white" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<LinearLayout 
android:id="@+id/lin_llt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

そして、私はこのような私の活動で線形レイアウトの背景を設定しました...

parentLayout = (LinearLayout) view.findViewById(R.id.lin_llt);
parentLayout.setBackgroundResource(R.drawable.bcd);

今、私がやりたいことは、ドローアブルリソースの色を変更したいということです。つまり、ドローアブルで定義された丸い角とパディングを使用して、線形レイアウトの色を変更します。

私はこれを次のように試しました

ShapeDrawable bgShape = (ShapeDrawable )parentLayout.getBackground();
bgShape.getPaint().setColor(Color.BLACK);

しかし、それは私にとってはうまくいきません。その他の解決策。

だからそれを行う方法...助けが必要です...ありがとう...

4

6 に答える 6

28

レイアウトの色を動的に変更する

LinearLayout Layout = (LinearLayout) findViewById(R.layout.id);
Layout.setBackgroundColor(Color.parseColor("#ffffff"));

背景色のグラデーションを動的に設定する

View layout = findViewById(R.id.mainlayout);

GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,
        new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);

layout.setBackgroundDrawable(gd);
于 2013-02-28T04:23:43.147 に答える
12

あなたはこのようなことを試すことができます:

Drawable sampleDrawable = context.getResources().getDrawable(R.drawable.balloons); 
sampleDrawable.setColorFilter(new PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));

詳細については、以下を参照してください。

AndroidでDrawableの色を変更するにはどうすればよいですか?

プログラムで描画可能な色を変更する

Android:実行時に形状の色を変更する

http://pastebin.com/Hd2aU4XC

これを試すこともできます:

private static final int[] FROM_COLOR = new int[]{49, 179, 110};
private static final int THRESHOLD = 3;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_colors);

    ImageView iv = (ImageView) findViewById(R.id.img);
    Drawable d = getResources().getDrawable(RES);
    iv.setImageDrawable(adjust(d));
}

private Drawable adjust(Drawable d)
{
    int to = Color.RED;

    //Need to copy to ensure that the bitmap is mutable.
    Bitmap src = ((BitmapDrawable) d).getBitmap();
    Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
    for(int x = 0;x < bitmap.getWidth();x++)
        for(int y = 0;y < bitmap.getHeight();y++)
            if(match(bitmap.getPixel(x, y))) 
                bitmap.setPixel(x, y, to);

    return new BitmapDrawable(bitmap);
}

private boolean match(int pixel)
{
    //There may be a better way to match, but I wanted to do a comparison ignoring
    //transparency, so I couldn't just do a direct integer compare.
    return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD && Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD && Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
}

AndroidでDrawableの色を変更する方法で与えられているように?

于 2013-02-28T04:15:39.290 に答える
3

以下は、形状を変更せずにプログラムでドローアブルの色を設定するのに最適です。

parentLayout.getBackground().setColorFilter(
    Color.BLACK,
    PorterDuff.Mode.SRC_ATOP
);
于 2018-03-27T14:10:56.277 に答える
2

これを使って..

<solid android:color="#e1e1e1" />

<stroke
    android:width="2dp"
    android:color="#808080" />

<corners android:radius="10dp" />

<padding
    android:bottom="5dp"
    android:left="5dp"
    android:right="5dp"
    android:top="5dp" />

于 2013-07-04T13:08:25.977 に答える
1

1つのアプローチは、2番目の色で2番目のドローアブルXMLを作成してから、2番目のドローアブルでレイアウトの背景を変更することです。

于 2013-02-28T04:16:21.250 に答える
1

また、可能な方法は次を使用することです。

val layerDrawable : LayerDrawable = imageView.background as LayerDrawable
val bgShape = layerDrawable.findDrawableByLayerId(R.id.shape_id) as GradientDrawable
bgShape.setColor(ContextCompat.getColor(context, R.color.colorPrimary))   

例(drawable / circle.xml):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/shape_id">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners android:radius="1000dp" />
        <solid android:color="#F4B528" />
        <padding
            android:bottom="4dp"
            android:left="4dp"
            android:right="4dp"
            android:top="4dp" />
    </shape>
</item>
</layer-list>

およびImageView

<ImageView
    android:id="@+id/imageView"
    android:layout_width="16dp"
    android:layout_height="16dp"
    android:background="@drawable/circle" 
/>
于 2020-07-15T16:00:50.813 に答える