TextView내 앱의 모든 인스턴스에 textColor="#ffffff". 각각에 대해 설정하는 대신 한곳에서 설정하는 방법이 TextView있습니까?
TextView내 앱의 모든 인스턴스에 textColor="#ffffff". 각각에 대해 설정하는 대신 한곳에서 설정하는 방법이 TextView있습니까?
답변:
실제로 사용자 정의 Java 클래스를 수행하거나 스타일을 개별적으로 설정할 필요없이 TextViews (및 대부분의 다른 내장 위젯)에 대한 기본 스타일을 설정할 수 있습니다.
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 이후의 장치에서 테스트되었으며 잘 작동하는 것 같습니다.
<item name="android:textViewStyle">@style/MyTextViewStyle</item>@Steve, MyTheme 스타일에 항목 을 포함하고 내 응용 프로그램에 스타일을 적용하면 모든 TextView스타일이 스타일에 따라 표시됩니다 (작업 표시 줄의 응용 프로그램 이름 포함). 작업 표시 줄로 문제를 해결하는 방법은 무엇입니까? 기본 모양을 갖기를 원합니다. Github
<item name="android:textViewStyle">@style/MyTextViewStyle</item>바로 액션 바의 기본 (또는 비슷한 것)을 가리키는, 너무 거기
두 가지 방법이 있습니다.
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 테마 및 스타일 이해
이것은 이것을 달성하는 또 다른 가능한 방법이며 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>
하나 또는 다른 선택을 사용하는지 여부는 필요에 따라 다릅니다. 원하는 유일한 작업이 모양을 수정하는 것이라면 가장 좋은 방법은 첫 번째 방법입니다. 반면에 모양을 변경하고 위젯에 몇 가지 새로운 기능을 추가하려면 두 번째 방법이 필요합니다.
custom_view_layout.xml사용자 정의 클래스를 확장하는 데 사용되는 파일 에서 사용할 수없는 이유는 무엇 입니까?
에서 기본 텍스트 색상 TextView설정, android:textColorTertiary원하는 색상 테마에 :
<item name="android:textColorTertiary">@color/your_text_color</item>
다른 많은 Android 컨트롤의 색상은 프레임 워크 속성을 사용하여 제어하거나 지원 라이브러리를 사용하는 경우 라이브러리 속성을 지원할 수 있습니다.
설정할 수있는 속성의 목록은 체크 아웃 안드로이드 소스 코드 의를 styles.xml하고 themes.xml, 또는이 매우 도움이 요점 댄 루하여 각 값을 변경 시도하고 그들이 화면에 변경 기능을 참조하십시오.