日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

android的padding属性,以编程方式获取android:padding属性

發(fā)布時間:2023/12/2 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android的padding属性,以编程方式获取android:padding属性 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

從一個角度來看,如何以編程方式獲取android:padding屬性的值? 我目前正在使用:

private static final String ANDROID_NAMESPACE = "http://schemas.android.com/apk/res/android"; private static final String ATTRIBUTE_PADDING = "padding"; public ActivityWrapperView(Context context, AttributeSet attrs) { super(context, attrs); int padding = attrs.getAttributeIntValue(ANDROID_NAMESPACE, ATTRIBUTE_PADDING, -1); }

這返回-1,我也嘗試使用“android:padding”作為屬性名稱,但仍然返回-1。

編輯:我的要求:當(dāng)在布局XML中指定android:padding值時,視圖將使用此填充。 如果未指定填充,則將使用默認(rèn)填充

最簡單的方法是使用android.R.styleable,如果它已經(jīng)可用的話。 與獲取自定義屬性的方式相同。 R.styleable是一個包含int數(shù)組屬性值的類。 因此,您需要創(chuàng)建自己的int數(shù)組,其中包含您需要的屬性的int值。

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

您可以將android:padding添加到自定義視圖的屬性中。

... ...

然后,您可以像訪問其他屬性一樣訪問該屬性:

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

你應(yīng)該看一下getPadding____()函數(shù)。

尺寸,填充和邊距

要測量其尺寸,視圖會考慮其填充。 填充以視圖的左,上,右和底部分的像素表示。 填充可用于將視圖的內(nèi)容偏移特定量的像素。 例如,左邊的填充為2會將視圖的內(nèi)容推到左邊緣右側(cè)2個像素。 可以使用setPadding(int,int,int,int)或setPaddingRelative(int,int,int,int)方法設(shè)置填充,并通過調(diào)用getPaddingLeft(),getPaddingTop(),getPaddingRight(),getPaddingBottom(),getPaddingStart( ),getPaddingEnd()。

即使視圖可以定義填充,它也不會為邊距提供任何支持。 但是,視圖組提供了這樣的支持。 有關(guān)詳細(xì)信息,請參閱ViewGroup和ViewGroup.MarginLayoutParams。

總結(jié)

以上是生活随笔為你收集整理的android的padding属性,以编程方式获取android:padding属性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。