2012-09-11 22 views
26

tôi đã cố gắng một cái gì đó như thế này, nhưng tôi bị mắc kẹt:làm thế nào để có được màu nền từ chủ đề hiện tại lập trình

TypedValue typedValue = new TypedValue(); 
if (this.parentActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true)) 
{ 
    // how to get color? 
} 
+9

tại xml android: background = "android:? Attr/colorBackground" – ademar111190

+2

Tôi đã thử nghiệm và xác định rằng 'android:? Attr/colorBackground' tương ứng với' mục styles.xml' ' @ color/yourColorHere'. –

+0

@ ademar111190 đây phải là câu trả lời !!!! – Alexandr

Trả lời

52

Bạn có thể nhận màu nền (hoặc Bản vẽ) từ chủ đề hiện tại theo:

TypedValue a = new TypedValue(); 
getTheme().resolveAttribute(android.R.attr.windowBackground, a, true); 
if (a.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT) { 
    // windowBackground is a color 
    int color = a.data; 
} else { 
    // windowBackground is not a color, probably a drawable 
    Drawable d = activity.getResources().getDrawable(a.resourceId); 
} 
+2

Câu trả lời hay. Trong trường hợp bất cứ ai khác bị vấp ngã bởi điều này, trên Xamarin bạn phải sử dụng Resource.Attribute.primaryAccentColor thay vì, ví dụ, Resource.Styleable.MyTheme_primaryAccentColor. Điều này cũng có thể áp dụng cho quá trình phát triển bản địa của Android. Đó là, tham khảo tập tin attr/đối tượng thay vì chủ đề trực tiếp. Tôi không hiểu sự khác biệt, nhưng sau này có vẻ như đó sẽ là lựa chọn đúng đắn nhưng KHÔNG. – pbarranis

4

Bạn có thể nhận được các nguồn lực của Theme của bạn bằng cách sử dụng:

TypedArray a = getTheme().obtainStyledAttributes(R.style.ThemeName, new int[] {R.attr.attribute_name});  
int attributeResourceId = a.getResourceId(0, 0); 
+0

Tôi đã thử điều này: TypedArray a = this.parentActivity.getTheme(). ObtainStyledAttributes (android.R.attr.windowBackground, new int [] {android.R.attr.windowBackground}); \t \t int attributeResourceId = a.getResourceId (0, 0); \t \t \t \t int aaa = this.parentActivity.getResources(). GetColor (attributeResourceId); nhưng điều này không hoạt động. Tôi bị ngoại lệ. –

+0

Oh! Bạn nhận được ngoại lệ gì? – Swayam

+0

09-12 12: 05: 34.864: E/AndroidRuntime (32137): LOẠI TRỪ BẤT HỢP: chính 09-12 12: 05: 34.864: E/AndroidRuntime (32137): android.content.res.Resources $ NotFoundException: Tệp res /drawable/screen_background_selector_light.xml từ ID tài nguyên danh sách trạng thái màu # 0x10804a8 09-12 12: 05: 34.864: E/AndroidRuntime (32137): \t tại android.content.res.Resources.loadColorStateList (Resources.java) 09- 12 12: 05: 34.864: E/AndroidRuntime (32137): \t tại android.content.res.Resources.getColor (Resources.java) ... –

1

cho qoustion của bạn theo cách dễ nhất là:

TypedValue typedValue = new TypedValue(); 
if (this.parentActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true)) 
{ 
    // how to get color? 
    int colorWindowBackground = typedValue.data;// **just add this line to your code!!** 
} 
Các vấn đề liên quan