android:layout_width="match_parent" Show android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New CheckBox" android:id="@+id/checkBox" /> <RadioButton android:id="@+id/radioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New RadioButton" /> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/seekBar" /> <RatingBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ratingBar" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" /> <Switch android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Switch" android:id="@+id/switch2" /> <ToggleButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New ToggleButton" android:id="@+id/toggleButton" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" /> <ProgressBar style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/progressBar" /> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:indeterminate="true" android:id="@+id/progressBar2" /> </LinearLayout> Android에서 스타일 및 테마를 사용하면 웹 디자인의 스타일시트와 유사하게 앱 디자인의 세부사항을 UI 구조 및 동작과 분리할 수 있습니다. 스타일은 단일 테마는 개별 뷰뿐만 아니라 앱, 활동 또는 뷰 계층 구조에 전체적으로 적용되는 속성 모음입니다. 테마를 적용하면 뷰에서 지원하는 각 테마 속성이 앱 또는 활동의 모든 뷰에 적용됩니다. 또한 테마를 통해 상태 표시줄 및 창 배경과 같이 뷰가 아닌 요소에도 스타일을 적용할 수 있습니다. 스타일 및 테마는 그림 1. 동일한 활동에 적용된 두 가지 테마: 테마와 스타일테마와 스타일은 유사점이 많지만 용도가 서로 다릅니다. 테마와 스타일은 속성을 리소스에 매핑하는 키-값 쌍이라는 동일한 기본 구조를 갖습니다. 스타일은 특정 뷰 유형의 속성을 지정합니다. 예를 들어 특정한 하나의 스타일을 사용해 버튼 속성을 지정할 수 있습니다. 스타일에 지정하는 모든 속성은 레이아웃 파일에서 설정할 수 있는 속성입니다. 모든 속성을 스타일로 추출하면 여러 위젯에서 손쉽게 속성을 사용하고 관리할 수 있습니다. 테마는 스타일, 레이아웃, 위젯 등으로 참조할 수 있는 명명된 리소스 모음을 정의합니다. 테마는 스타일 및 테마는 함께 작동하도록 설계되었습니다. 예를 들어 버튼의 한 부분은 테마와 스타일이 함께 작동하는 방법에 관한 자세한 내용은 Android 스타일 지정: 테마와 스타일 블로그 게시물을 참고하세요. 스타일 만들기 및 적용새 스타일 또는 테마를 만들려면 프로젝트의
예를 들어 다음과 같은 스타일을 정의한다면 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="GreenText" parent="TextAppearance.AppCompat"> <item name="android:textColor">#00FF00</item> </style> </resources> 다음과 같이 스타일을 뷰에 적용할 수 있습니다. <TextView style="@style/GreenText" ... /> 스타일에 지정된 각 속성을 뷰에서 허용하면 속성이 뷰에 적용됩니다. 뷰에서 허용하지 않는 모든 속성은 무시되고 적용되지 않습니다. 참고: 그러나 개별 뷰에 스타일을 적용하는 대신 일반적으로 전반적인 앱, 활동 또는 뷰 모음에 스타일을 테마로 적용할 수 있습니다. 스타일 확장 및 맞춤설정고유한 스타일을 만들 때는 플랫폼 UI 스타일과의 호환성을 유지할 수 있도록 항상 프레임워크 또는 지원 라이브러리의 기존 스타일을 확장해야 합니다. 스타일을 확장하려면 예를 들어 Android 플랫폼의 기본 텍스트 모양을 상속받아 다음과 같이 수정할 수 있습니다. <style name="GreenText" parent="@android:style/TextAppearance"> <item name="android:textColor">#00FF00</item> </style> 그러나 항상 Android 지원 라이브러리에서 핵심 앱 스타일을 상속받아야 합니다. 지원 라이브러리의 스타일은 각 버전에서 사용 가능한 UI 속성에 맞춰 각 스타일을 최적화함으로써 Android 4.0(API 수준 14) 이상과의 호환성을 제공합니다. 지원 라이브러리 스타일의 이름은 흔히 플랫폼의 기존 스타일과 비슷하지만 라이브러리 또는 자체 프로젝트에서 스타일을 상속받으려면
위에 표시된 <style name="GreenText" parent="TextAppearance.AppCompat"> <item name="android:textColor">#00FF00</item> </style> 또한 <style name="GreenText.Large"> <item name="android:textSize">22dp</item> </style> 더 많은 이름을 체인으로 연결하면 원하는 만큼 여러 번 이와 같은 방식으로 스타일을 계속 상속받을 수 있습니다. 참고: 점 표기법을 사용하여 스타일을 확장하고
스타일을 테마로 적용스타일을 만들 때와 동일한 방식으로 테마를 만들 수 있습니다. 차이점은 적용 방식입니다. 뷰에서 예를 들어 다음은 Android 지원 라이브러리의 머티리얼 디자인 '어두운' 테마를 앱 전체에 적용하는 방법입니다. <manifest ... > <application android:theme="@style/Theme.AppCompat" ... > </application> </manifest> 다음은 하나의 활동에만 '밝은' 테마를 적용하는 방법입니다. <manifest ... > <application ... > <activity android:theme="@style/Theme.AppCompat.Light" ... > </activity> </application> </manifest> 이제 앱 또는 활동의 모든 뷰에는 지정된 테마에 정의된 스타일이 적용됩니다. 뷰가 스타일에 선언된 속성의 일부만 지원하면 뷰에는 지원되는 속성만 적용되고 지원되지 않는 속성은 무시됩니다. Android 5.0(API 수준 21) 및 Android 지원 라이브러리 v22.1부터는 레이아웃 파일의 뷰에 앞의 예는 Android 지원 라이브러리에서 제공하는 스타일 계층 구조Android에서는 Android 앱 전체에 걸쳐 여러 방식으로 속성을 설정할 수 있습니다. 예를 들어 레이아웃에서 직접 속성을 설정하고 뷰에 스타일을 적용하며 레이아웃에 테마를 적용하고 프로그래매틱 방식으로 속성을 설정할 수도 있습니다. 앱 스타일 지정 방법을 선택할 때 Android의 스타일 계층 구조에 유의해야 합니다. 일반적으로 테마 및 스타일을 최대한 일관되게 사용해야 합니다. 여러 위치에 동일한 속성을 지정했다면 최종적으로 적용되는 속성은 아래 목록에 따라 결정됩니다. 목록은 가장 높은 우선순위에서 가장 낮은 순서로 정렬되어 있습니다.
그림 2. 앱 스타일을 지정하려고 하는데 예상한 결과가 표시되지 않는다면 다른 스타일 지정으로 인해 변경사항이 재정의되었을 가능성이 큽니다. 예를 들어 개별 TextAppearance스타일의 한 가지 제한사항은 하나의 스타일만 <TextView ... android:textAppearance="@android:style/TextAppearance.Material.Headline" android:text="This text is styled via textAppearance!" />
포함되지 않는 일반적인 기본 테마 맞춤설정Android 스튜디오를 사용하여 프로젝트를 만들 때 프로젝트의 예를 들어 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> 스타일 값은 실제로 프로젝트의 색상을 알게 되면 다음과 같이 <?xml version="1.0" encoding="utf-8"?> <resources> <!-- color for the app bar and other primary UI elements --> <color name="colorPrimary">#3F51B5</color> <!-- a darker variant of the primary color, used for the status bar (on Android 5.0+) and contextual app bars --> <color name="colorPrimaryDark">#303F9F</color> <!-- a secondary color for controls like checkboxes and text fields --> <color name="colorAccent">#FF4081</color> </resources> 그런 다음 원하는 다른 스타일을 재정의할 수 있습니다. 예를 들어 다음과 같이 활동 배경 색상을 변경할 수 있습니다. <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> ... <item name="android:windowBackground">@color/activityBackground</item> </style> 테마에 사용할 수 있는 속성 목록은 대부분의 속성은 특정 유형의 뷰에 적용되며 일부 속성은 모든 뷰에 적용됩니다. 그러나 또한 Android 지원 라이브러리는 참고: 지원 라이브러리의 속성 이름은 또한 지원 라이브러리에서는 위에 나온 테마 대신 확장할 수 있는 다양한 테마도 제공합니다. 사용 가능한 주요 테마는 라이브러리의 themes.xml 파일에서 확인할 수 있습니다. 버전별 스타일 추가새 버전의 Android에 사용하려는 테마 속성을 추가할 때 기존 테마에 이 속성을 추가할 수 있으며 이는 이전 버전과 계속 호환됩니다. 리소스 버전 한정자를 포함하는 res/values/styles.xml # themes for all versions res/values-v21/styles.xml # themes for API level 21+ only
예를 들어 Android 5.0(API 수준 21) 이상에서 창 전환을 선언하려면 몇 가지 새로운 속성을 사용해야 합니다. 따라서 <resources> <!-- base set of styles that apply to all versions --> <style name="BaseAppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorPrimary">@color/primaryColor</item> <item name="colorPrimaryDark">@color/primaryTextColor</item> <item name="colorAccent">@color/secondaryColor</item> </style> <!-- declare the theme name that's actually applied in the manifest file --> <style name="AppTheme" parent="BaseAppTheme" /> </resources> 이어서 다음과 같이 <resources> <!-- extend the base theme to add styles available only with API level 21+ --> <style name="AppTheme" parent="BaseAppTheme"> <item name="android:windowActivityTransitions">true</item> <item name="android:windowEnterTransition">@android:transition/slide_right</item> <item name="android:windowExitTransition">@android:transition/slide_left</item> </style> </resources> 이제 매니페스트 파일에서 다양한 기기에 대체 리소스를 사용하는 방법에 관한 자세한 내용은 리소스 제공을 참고하세요. 프레임워크 및 지원 라이브러리의 모든 위젯에는 기본 스타일이 있습니다. 예를 들어 지원 라이브러리의 테마를 사용하여 앱의 스타일을 지정할 때 <Button style="@style/Widget.AppCompat.Button.Borderless" ... /> 이 스타일을 모든 버튼에 적용하려면 다음과 같이 테마의 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="buttonStyle">@style/Widget.AppCompat.Button.Borderless</item> ... </style> 또한 다른 모든 스타일을 확장하는 것처럼 위젯 스타일도 확장할 수 있습니다. 그런 다음 레이아웃 또는 테마에 맞춤 위젯 스타일을 적용할 수 있습니다. 추가 리소스테마와 스타일을 자세히 알아보려면 다음 추가 리소스를 참고하세요. 블로그 게시물
|