日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/2 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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。

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

最簡單的方法是使用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);

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

尺寸,填充和邊距

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

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

總結(jié)

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

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