Android Drawable: Specifying shape width in percent in the XML file?

前端 未结 5 1501
攒了一身酷
攒了一身酷 2020-12-06 04:06

I\'m trying to create a simple Drawable that I want to set as the background for a view (using setBackgroundDrawable). I simply want to divide the background of the

5条回答
  •  醉梦人生
    2020-12-06 05:01

    The way I found to do this was by creating my own class inheriting from RelativeLayout, where I override the onMeasure method. At this stage, the height of the view is known, so I fetched the background, and manually set the inset of the items using setLayerInset.

        LayerDrawable bg = (LayerDrawable)getBackground();
        int h = getMeasuredHeight();
        bg.setLayerInset(1, 0, 0, 0, h/2);
    

    This method is a bit cumbersome though. In most cases, the answer provided by chris should be sufficient.

提交回复
热议问题