setBackgroundResource causes Padding to fail, settextsize

The padding is reset when the 9 patch map is set via setBackground. At this time, you need to call the getPadding method before setBackground to get the padding before it is reset, and then reset the padding with code after calling setBackground.
    /**
     * Setting the background in the code will cause the padding to fail
     * @param rl
     */
    private void setRlPadding(RelativeLayout rl) {
        int top = rl.getPaddingTop();
        int bottom = rl.getPaddingBottom();
        int left = rl.getPaddingLeft();
        int right = rl.getPaddingRight();
        rl.setBackgroundResource(R.drawable.hive_journey_bg);
        rl.setPadding(left,top,right,bottom);
    }

This only happens when the background image is .9, not otherwise.

There is also a small detail to record here by the way. Many students find that the font size set by the setTextSize method of TextView is always different from what they think. The reason is that the unit of settextSize is dp by default. The first thing that comes to mind at this time is to directly write setTextSize(10) to death, but this will cause trouble for the configuration of common fonts for the entire application. If you want to change the font size one day, this textview is very difficult. easy to miss. So we want to put the size of this font into the configuration file, so we have to call this method
titleTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.hive_textsize_1));

The first parameter of this method is the type that specifies the size, such as dp or px. The key lies in the second parameter, via
getResources().getDimension(R.dimen.hive_textsize_1)

The size obtained by this method is actually converted by the system, if we write this in the configuration file
<dimen name="hive_textsize_1">16sp</dimen>
. Then the size obtained by the getResources method is converted according to the screen density, so if the font is to be displayed normally, the size type must be set to px. Of course, if the unit we define is px, there will be no conversion here.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327076256&siteId=291194637