티스토리 뷰
안드로이드 개발을 하다보면 프로그래밍적으로 xml로 정의된 theme와 style에 접근을 하여야 하는 경우가 있습니다.
이러한 경우에 소스상에서 값을 가져오는 방법입니다.
1. 현재 theme 속성 resource id 가져오기
xml에서는 ?attr:windowTitleStyle로 windowTitleStyle의 값을 가져올수 있지만, 이것을 코드에서 사용하면 다음과 같습니다.
View안에서 사용할 경우에 다음과 같습니다.
Activity에서 안된다고 하지 말기~~
TypedValue typeValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.windowTitleStyle, outValue, true);
int resId = typeValue.resourceId;
2. 특정 style의 특정 속성 가져오기
이것도 View안에서임. getContext()는 Activity에 없음.
TypedArray typedArray = getContext().getTheme().obtainStyledAttributes( android.R.style.TextAppearance_DialogWindowTitle, new int[]{android.R.attr.textColor, android.R.attr.textSize});
int textColor = typedArray.getColor(0,0);
int textSize = typedArray.getDimensionPixelSize(1,0);
typedArray.recycle();
주의사항은. 꼭 사용하고 recycle을 하여줍시다.
3. 소스에서 테마 적용하기
public TestTextView(Context context, AttrivuteSet attrs) {
super(context, attrs, R.attr.testTheme);
}
'Mobile > Android' 카테고리의 다른 글
[Android]터치이벤트 발생 (0) | 2015.04.17 |
---|---|
[Android]ActionBar Height (0) | 2015.04.17 |
[Android]Eclipse to Studio (0) | 2015.04.16 |
[Android]Style, Theme (0) | 2015.04.14 |
[Android]FragmentPagerAdapter, FragmentStatePagerAdapter의 차이 (0) | 2015.04.14 |
- Total
- Today
- Yesterday
- intellij
- Python
- windows10
- Kotlin
- golang
- tomcat
- Windows
- Codable
- ios
- war
- Linux
- centos8
- rxswift
- ubuntu
- docker
- CentOS
- MySQL
- nodejs
- git
- Java
- go
- php
- Xcode
- cocoapods
- Spring
- enum
- android
- github
- Gradle
- SWIFT
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |