programing tip

Android에서 뷰의 전역 스타일 설정

itbloger 2020. 7. 19. 10:41
반응형

Android에서 뷰의 전역 스타일 설정


TextView내 앱의 모든 인스턴스에을 원한다고 가정 해 봅시다 textColor="#ffffff". 각각에 대해 설정하는 대신 한곳에서 설정하는 방법이 TextView있습니까?


실제로, 사용자 정의 Java 클래스를 수행하거나 스타일을 개별적으로 설정하지 않고도 TextView (및 대부분의 다른 내장 위젯)의 기본 스타일을 설정할 수 있습니다.

themes.xmlAndroid 소스를 살펴보면 다양한 위젯의 기본 스타일에 대한 많은 속성이 표시됩니다. 키는 사용자 정의 테마에서 재정의 하는 textViewStyle(또는 editTextStyle등) 속성입니다. 다음과 같은 방법으로이를 무시할 수 있습니다.

styles.xml: 만들기

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
    <item name="android:textViewStyle">@style/MyTextViewStyle</item>
</style>

<style name="MyTextViewStyle" parent="android:Widget.TextView">
    <item name="android:textColor">#F00</item>
    <item name="android:textStyle">bold</item>
</style>
</resources>

그런 다음 해당 테마를 응용 프로그램에 적용하십시오 AndroidManifest.xml.

<application […] android:theme="@style/MyTheme">…

그리고 모든 텍스트보기는 기본적으로 정의 된 스타일 MyTextViewStyle(이 경우 굵은 체와 빨간색)으로 설정됩니다!

이것은 API 레벨 4 이후의 장치에서 테스트되었으며 훌륭하게 작동하는 것 같습니다.


그렇게하는 두 가지 방법이 있습니다.

1. 스타일 사용

res/values디렉토리 에서 XML 파일을 작성하여 고유 한 스타일을 정의 할 수 있습니다 . 따라서 빨간색과 굵은 텍스트를 원한다고 가정 하고이 내용으로 파일을 만듭니다.

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="MyRedTheme" parent="android:Theme.Light">
    <item name="android:textAppearance">@style/MyRedTextAppearance</item>
  </style>
  <style name="MyRedTextAppearance" parent="@android:style/TextAppearance">
    <item name="android:textColor">#F00</item>
    <item name="android:textStyle">bold</item>
  </style>
</resources>

예를 들어 원하는대로 이름을 지정할 수 있습니다 res/values/red.xml. 그런 다음, 원하는 위젯에서 해당보기를 사용하는 것만하면됩니다 (예 :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    style="@style/MyRedTheme"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="This is red, isn't it?"
    />
</LinearLayout>

추가 참조를 위해이 기사를 읽을 수 있습니다. Android 테마 및 스타일 이해

2. 커스텀 클래스 사용하기

이것은 이것을 달성하는 또 다른 가능한 방법이며 TextView, 텍스트 색상을 항상 원하는대로 설정하는 자신 만의 방법을 제공하는 것입니다 . 예를 들어 :

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.TextView;
public class RedTextView extends TextView{
    public RedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setTextColor(Color.RED);
    }
}

그런 다음 TextViewXML 파일에서 일반적인 것으로 취급하면됩니다 .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<org.example.RedTextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="This is red, isn't it?"
    />
</LinearLayout>

하나 또는 다른 선택을 사용하는지 여부는 필요에 따라 다릅니다. 원하는 유일한 모양을 수정하는 것이 가장 좋은 방법은 첫 번째 방법입니다. 반면에 모양을 변경하고 위젯에 새로운 기능을 추가하려면 두 번째 방법이 있습니다.


의 기본 텍스트 색상을 테마에서 원하는 색상으로 TextView설정 android:textColorTertiary하십시오.

<item name="android:textColorTertiary">@color/your_text_color</item>

지원 라이브러리를 사용하는 경우 프레임 워크 속성을 사용하거나 지원 라이브러리 속성을 사용하여 다른 많은 Android 컨트롤의 색상을 제어 할 수 있습니다.

For a list of attributes you can set, check out Android source code of styles.xml and themes.xml, or this very helpful gist by Dan Lew, try changing each value and see what they change on screen.


Define a style and use it on each widget, define a theme that overrides the android default for that widget, or define a string resource and reference it in each widget

참고URL : https://stackoverflow.com/questions/3078081/setting-global-styles-for-views-in-android

반응형