Dynamically change the color of the action bar separator (android: bottom for programmatically generated ShapeDrawable)?

I am trying to change the color of the separator bar at the bottom of the action bar programmatically. My strategy is to set the action bar background to a programmatically generated LayerDrawable containing ShapeDrawable rectangles based on this XML :

 <?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> 

But I got to the checkpoint: I can’t understand how to apply the android:bottom property (as in <item android:bottom="2dip"> ) programmatically. Obviously, android:bottom is a property of the item tag, to which (I think) there is no software equivalent, and I could not find any ShapeDrawable methods / properties that look appropriate.

Code so far:

 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; } 

Ideas? If it matters for alternative solutions, I use ActionBarSherlock.

EDIT:

setLayerInset, as suggested by MH, did what I wanted. A modified version of the function used is used here:

 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
source share
4 answers

According to the previous comment:

Did you give setLayerInset(int index, int l, int t, int r, int b) to try? The docs say:

Specify border modifiers for the selected [index]. left + = l top + = t; right - = r; bottom - = b;

+3
source

Judging by the source code for LayerDrawable, the method that allows you to do this is confidential. But you could accomplish this with a LayerDrawable containing an InsetDrawable

+1
source

If you can clarify, for what reason do you change the color programmatically? It seems a little pointless if you don't control the background color. If you control the background color, you are probably using styles, in which case I can come up with several ways to do this.

Simon

0
source

This worked for me:

 <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> 

Then call:

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

You need to make sure you have installed android: id

0
source

Source: https://habr.com/ru/post/1488566/


All Articles