textApperanceLargeのint値をコードで取得したいと思います。以下のコードは正しい方向に進んでいると思いますが、TypedValueからint値を抽出する方法を理解できません。
TypedValue typedValue = new TypedValue();
((Activity)context).getTheme().resolveAttribute(android.R.attr.textAppearanceLarge, typedValue, true);
textApperanceLargeのint値をコードで取得したいと思います。以下のコードは正しい方向に進んでいると思いますが、TypedValueからint値を抽出する方法を理解できません。
TypedValue typedValue = new TypedValue();
((Activity)context).getTheme().resolveAttribute(android.R.attr.textAppearanceLarge, typedValue, true);
回答:
コードは、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であるという事実に由来しています。
使用する
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);
@ 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のように、取得しようとしているタイプに注意してください。
これは私のコードです。
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