Android:コードで属性の値を取得する方法は?


82

textApperanceLargeのint値をコードで取得したいと思います。以下のコードは正しい方向に進んでいると思いますが、TypedValueからint値を抽出する方法を理解できません。

TypedValue typedValue = new TypedValue(); 
((Activity)context).getTheme().resolveAttribute(android.R.attr.textAppearanceLarge, typedValue, true);

回答:


127

コードは、textAppearanceLarge属性が指すスタイルのリソースID 、つまりRenoが指摘するTextAppearance.Largeのみを取得します。

スタイルからtextSize属性値を取得するには、次のコードを追加するだけです。

int[] textSizeAttr = new int[] { android.R.attr.textSize };
int indexOfAttrTextSize = 0;
TypedArray a = context.obtainStyledAttributes(typedValue.data, textSizeAttr);
int textSize = a.getDimensionPixelSize(indexOfAttrTextSize, -1);
a.recycle();

これで、textSizeは、textApperanceLargeが指すスタイルのピクセル単位のテキストサイズになります。設定されていない場合は-1になります。これは、typedValue.typeが最初からTYPE_REFERENCE型であると想定しているため、最初に確認する必要があります。

番号16973890は、TextAppearance.LargeのリソースIDであるという事実に由来しています


6
チャームのように機能します。なぜそれがそれほど複雑でなければならないのか... 6年後の今までにそれほど曖昧なアプローチはありませんか?
Cee McSharpface 2017

54

使用する

  TypedValue typedValue = new TypedValue(); 
  ((Activity)context).getTheme().resolveAttribute(android.R.attr.textAppearanceLarge, typedValue, true);

文字列の場合:

typedValue.string
typedValue.coerceToString()

その他のデータの場合:

typedValue.resourceId
typedValue.data  // (int) based on the type

あなたの場合、それが返すものはですTYPE_REFERENCE

TextAppearance.Largeを指す必要があることはわかっています

これは:

<style name="TextAppearance.Large">
    <item name="android:textSize">22sp</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textColor">?textColorPrimary</item>
</style>

これを解決したのはMartinの功績です。

int[] attribute = new int[] { android.R.attr.textSize };
TypedArray array = context.obtainStyledAttributes(typedValue.resourceId, attribute);
int textSize = array.getDimensionPixelSize(0, -1);

1
typedValue.dataの評価は16973890です。テキストサイズの場合、これは正しくないようです。
ab11 2011年

@ ab11テキストサイズではありません。これは、ディメンションリソースの整数です。
SevastyanSavanyuk19年

9

またはkotlinの場合:

fun Context.dimensionFromAttribute(attribute: Int): Int {
    val attributes = obtainStyledAttributes(intArrayOf(attribute))
    val dimension = attributes.getDimensionPixelSize(0, 0)
    attributes.recycle()
    return dimension
}

4

@ user3121370の回答に対する異端審問のようです。彼らは全焼した。:O

パディングなどのディメンションを取得する必要がある場合は、minHeight(私の場合はandroid.R.attr.listPreferredItemPaddingStart)。できるよ:

TypedValue typedValue = new TypedValue(); 
((Activity)context).getTheme().resolveAttribute(android.R.attr.listPreferredItemPaddingStart, typedValue, true);

質問がしたように、そして次に:

final DisplayMetrics metrics = new android.util.DisplayMetrics();
WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
int myPaddingStart = typedValue.getDimension( metrics );

削除された答えのように。これにより、デフォルトのデバイスメトリックが使用されるため、デバイスのピクセルサイズの処理をスキップできます。戻り値はfloatになり、intにキャストする必要があります。

resourceIdのように、取得しようとしているタイプに注意してください。


1

これは私のコードです。

public static int getAttributeSize(int themeId,int attrId, int attrNameId)
{
    TypedValue typedValue = new TypedValue();
    Context ctx = new ContextThemeWrapper(getBaseContext(), themeId);

    ctx.getTheme().resolveAttribute(attrId, typedValue, true);

    int[] attributes = new int[] {attrNameId};
    int index = 0;
    TypedArray array = ctx.obtainStyledAttributes(typedValue.data, attributes);
    int res = array.getDimensionPixelSize(index, 0);
    array.recycle();
    return res;
} 

// getAttributeSize(theme, android.R.attr.textAppearanceLarge, android.R.attr.textSize)   ==>  return android:textSize
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.