| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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 | 29 | 30 | 31 | 
													Tags
													
											
												
												- 안드로이드 갤러리 접근
- 습관만들기
- coroutine
- OkHttp Interceptor
- Android ViewPager2
- Android 12 대응
- Android Jetpack
- 66챌린지
- MVP Architecture
- Kotlin FCM
- Android ProgressBar
- Android 12
- DataBinding
- 알고리즘 자바
- 안드로이드 카카오 로그인
- android recyclerview
- WebView
- Java
- 카카오 알고리즘
- Android Navigation
- 안드로이드
- 프로그래머스 알고리즘
- 영어독립365
- 코틀린 코루틴
- 영어공부
- Kotlin
- Android WebView
- Android Interceptor
- Android
- scope function
													Archives
													
											
												
												- Today
- Total
나미래 Android 개발자
안드로이드 style.xml 사용 예제 본문
style.xml을 이용해 중복을 제거하자
아래 코드를 보면 TextView 와 EditText 에서 속성 값으로 {fontFamily,  textColor, textSize} 가 중복되어 사용되는 것을 볼 수 있다. 만약 아래와 같은 TextView 또는 EditText가 100개 이상이 있다면 중복으로 인해 코드가 불필요하게 많아진다.
이를 해결하기 위해서 style.xml 을 사용했다.
style.xml 사용 전 - /res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingHorizontal="20dp"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/name_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/roboto"
        android:text="@string/name"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp" />
    <EditText
        android:id="@+id/editText_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/roboto"
        android:hint="@string/what_is_your_nickname"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp" />
</LinearLayout>style.xml 사용 후 - /res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingHorizontal="20dp"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/name_text"
        style="@style/NameStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/name"
        android:textAlignment="center" />
    <EditText
        android:id="@+id/editText_name"
        style="@style/NameStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/what_is_your_nickname"
        android:textAlignment="center" />
</LinearLayout>style.xml 파일 만들기
/res/values 폴더를 우클릭 한다.
New/Value Resource File 를 클릭한다.
New Resource File 마법사에서 File name 에 style.xml을 Directory name에 values를 입력하고 OK 를 클릭한다.
style.xml에 style 추가하기 - /res/values/style.xml
<resources>
    <style name="NameStyle">
        <item name="android:fontFamily">@font/roboto</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:textSize">20sp</item>
    </style>
</resources>'안드로이드' 카테고리의 다른 글
| 안드로이드 원형 버튼 만들기 (4) | 2021.11.08 | 
|---|---|
| Android 키보드 생성 시, Bottom Navigation Hide (0) | 2021.10.28 | 
| 안드로이드 문자열 리소스(strings.xml) 사용 예제 (0) | 2021.10.19 | 
| 안드로이드 카카오 SDK V2 로그인 - part2(코드) (0) | 2021.09.27 | 
| 안드로이드 카카오 SDK V2 로그인 - part1(프로젝트 셋업) (0) | 2021.09.27 | 
			  Comments
			
		
	
               
           
					
					
					
					
					
					
				 
								 
								