Get android:padding attribute programmatically

前端 未结 3 1582

From a view, how can I get the value of the android:padding attribute programmatically? I am currently using:

private static final String ANDROID_NAMESPACE = \"h         


        
相关标签:
3条回答
  • 2021-02-07 06:37

    You can just add the android:padding to your custom view's attributes.

    <declare-styleable name="ActivityWrapperView">
        ...
        <attr name="android:padding" />
        ...
    </declare-styleable>
    

    And then, you can access the attribute just like your other attributes:

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActivityWrapperView);
    float padding = a.getDimension(R.styleable.ActivityWrapperView_android_padding, 0);
    ...
    boolean hasPadding = a.hasValue(R.styleable.ActivityWrapperView_android_padding);
    
    0 讨论(0)
  • 2021-02-07 06:39

    The easiest way was to use android.R.styleable, if it had had been available. The same way it uses for getting custom attributes. R.styleable is a class which contains an int arrays of attributes values. So you need to create your own int array, which contains int values of atributes you need.

    public ActivityWrapperView(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        //check attributes you need, for example all paddings
        int [] attributes = new int [] {android.R.attr.paddingLeft, android.R.attr.paddingTop, android.R.attr.paddingBottom, android.R.attr.paddingRight}
    
        //then obtain typed array
        TypedArray arr = context.obtainStyledAttributes(attrs, attributes);
    
        //and get values you need by indexes from your array attributes defined above
        int leftPadding = arr.getDimensionPixelOffset(0, -1);
        int topPadding = arr.getDimensionPixelOffset(1, -1);
    
        //You can check if attribute exists (in this examle checking paddingRight)
        int paddingRight = arr.hasValue(3) ? arr.getDimensionPixelOffset(3, -1) : myDefaultPaddingRight;
    
    
    }
    

    EDIT as per @Eselfar's comment:
    Don't forget to release the TypedArray: arr.recycle() !

    0 讨论(0)
  • 2021-02-07 06:42

    You should look at the getPadding____() functions.

    Size, padding and margins

    ...

    To measure its dimensions, a view takes into account its padding. The padding is expressed in pixels for the left, top, right and bottom parts of the view. Padding can be used to offset the content of the view by a specific amount of pixels. For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge. Padding can be set using the setPadding(int, int, int, int) or setPaddingRelative(int, int, int, int) method and queried by calling getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom(), getPaddingStart(), getPaddingEnd().

    Even though a view can define a padding, it does not provide any support for margins. However, view groups provide such a support. Refer to ViewGroup and ViewGroup.MarginLayoutParams for further information.

    0 讨论(0)
提交回复
热议问题