Android: Làm thế nào để lấy giá trị của một thuộc tính trong mã?


82

Tôi muốn lấy giá trị int của textApperanceLarge trong mã. Tôi tin rằng đoạn mã dưới đây đang đi đúng hướng, nhưng không thể tìm ra cách trích xuất giá trị int từ TypedValue.

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

Tương tự với Truy cập phần
đính kèm

Câu trả lời:


127

Mã của bạn chỉ nhận được ID tài nguyên của kiểu mà thuộc tính textAppearanceLarge trỏ tới, cụ thể là TextAppearance.Large như Reno chỉ ra.

Để nhận giá trị thuộc tính textSize từ kiểu, chỉ cần thêm mã này:

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

Bây giờ textSize sẽ là kích thước văn bản tính bằng pixel của kiểu mà textApperanceLarge trỏ đến, hoặc -1 nếu nó không được đặt. Điều này giả sử rằng typedValue.type là loại TYPE_REFERENCE để bắt đầu, vì vậy bạn nên kiểm tra điều đó trước.

Số 16973890 xuất phát từ thực tế rằng nó là ID tài nguyên của TextAppearance.


6
hoạt động như một sự quyến rũ. chỉ là tại sao nó phải phức tạp như vậy ... có phải bây giờ, sáu năm sau, có không ít cách tiếp cận mù mờ?
Cee McSharpface

54

Sử dụng

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

Đối với chuỗi:

typedValue.string
typedValue.coerceToString()

Đối với dữ liệu khác:

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

Trong trường hợp của bạn, những gì nó trả về là của TYPE_REFERENCE.

Tôi biết nó sẽ trỏ đến TextAppearance.

Đó là:

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

Tín dụng được chuyển đến Martin để giải quyết vấn đề này:

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

1
typedValue.data đánh giá là: 16973890. Điều này có vẻ không đúng, đối với kích thước văn bản.
ab11

@ ab11 nó không phải là kích thước văn bản. Nó là số nguyên của tài nguyên thứ nguyên.
Sevastyan Savanyuk

9

Hoặc trong kotlin:

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

4

Có vẻ như đây là một cuộc điều tra về câu trả lời của @ user3121370. Chúng bị thiêu rụi. : O

Nếu bạn chỉ cần lấy một thứ nguyên, như padding, minHeight (trường hợp của tôi là: android.R.attr.listPreferredItemPaddingStart). Bạn có thể làm:

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

Giống như câu hỏi đã làm, và sau đó:

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

Cũng giống như câu trả lời đã bị loại bỏ. Điều này sẽ cho phép bạn bỏ qua việc xử lý kích thước pixel thiết bị vì nó sử dụng chỉ số thiết bị mặc định. Kết quả trả về sẽ là float, và bạn nên ép kiểu int.

Hãy trung thành với loại mà bạn đang cố gắng đạt được, như resourceId.


1

đây là mã của tôi.

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
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.