Programing

사용자 정의 테마를 사용하여 기본 텍스트 색상을 변경하는 방법은 무엇입니까?

lottogame 2021. 1. 7. 07:36
반응형

사용자 정의 테마를 사용하여 기본 텍스트 색상을 변경하는 방법은 무엇입니까?


내가 시도하는 것은 테마로 매우 쉬워야하지만 방법을 찾을 수 없습니다. 내 앱에서 기본적으로 모든 텍스트를 흰색으로 설정하고 싶습니다. theme.xml에 사용자 정의 테마를 만들었습니다.

<style name="Theme" parent="@android:Theme">
</style>

<style name="TextAppearance.Theme" parent="@android:TextAppearance.Theme">
    <item name="android:textColor">#ffffffff</item>
</style>

전체 애플리케이션에 대해 설정합니다.

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/Theme">

그러나 레이블은 여전히 ​​검은 색입니다. 무엇이 빠졌습니까?

추신 : 위젯별로 적용 할 다양한 텍스트 크기의 스타일을 추가로 정의하려면 어떻게해야합니까? 그게 맞나요?

<style name="Theme.smallText">
    <item name="android:textSize">12dp</item>
</style>

최신 정보

Android SDK 에서 themes.xml살펴 보았는데 테마의 텍스트 스타일을 설정하는 방법을 보여줍니다.

<item name="textAppearance">@android:style/TextAppearance</item>

제 경우에는 다음 정의와 함께 작동해야합니다.

<style name="Theme" parent="@android:Theme">
    <item name="android:textAppearance">@style/MyText</item>
</style>

<style name="MyText" parent="@android:style/TextAppearance">
    <item name="android:textColor">#ffffffff</item>
</style>

그러나 여전히 작동하지 않습니다.

여기 또 다른 게시물 이 같은 문제에 대한이.


당신에 Manifest당신의 이름을 참조 할 필요가 style텍스트 색이 item그 안에 있습니다. 지금은 빈 style. 따라서 theme.xml에서 다음 만 수행하십시오 style.

<style name="Theme" parent="@android:style/TextAppearance">
    <item name="android:textColor">#ffffffff</item>
</style>

그리고 Manifest같은 ( android:theme="@style/Theme") 에 대한 참조를 유지하십시오.

편집 :

theme.xml :

<style name="MyTheme" parent="@android:style/TextAppearance">
    <item name="android:textColor">#ffffffff</item>
    <item name="android:textSize">12dp</item>
</style>

명백한:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/MyTheme">

텍스트 색상과 크기를 동일한 style. 또한 테마의 이름을 MyTheme로 변경했으며 이제 Manifest. 그리고 변경 @android:style/TextAppearance에 대한 parent값입니다.


앱을 생성하면 res / values ​​폴더에 styles.xml이라는 파일이 생성됩니다. 스타일을 변경하면 모든 레이아웃의 배경, 텍스트 색상 등을 변경할 수 있습니다. 이렇게하면 개별 레이아웃으로 이동하여 수동으로 변경할 필요가 없습니다.

styles.xml :

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.AppBaseTheme" parent="@android:style/Theme.Light">
    <item name="android:editTextColor">#295055</item> 
    <item name="android:textColorPrimary">#295055</item>
    <item name="android:textColorSecondary">#295055</item>
    <item name="android:textColorTertiary">#295055</item>
    <item name="android:textColorPrimaryInverse">#295055</item>
    <item name="android:textColorSecondaryInverse">#295055</item>
    <item name="android:textColorTertiaryInverse">#295055</item>

     <item name="android:windowBackground">@drawable/custom_background</item>        
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

parent="@android:style/Theme.Light"Google의 기본 색상입니다. 다음은 기본 스타일에 대한 참조입니다. https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

기본 Android 스타일은 "테마"라고도합니다. 그래서 당신은 그것을 Theme라고 부르면 아마도 프로그램을 혼란스럽게 할 것입니다.

name="Theme.AppBaseTheme"에서 모든 스타일을 상속하는 스타일을 만들고 있음을 의미합니다 parent="@android:style/Theme.Light". 이 부분은 AppBaseTheme에서 다시 상속하지 않으려면 무시할 수 있습니다. =<style name="AppTheme" parent="AppBaseTheme">

@ drawable / custom_background는 드로어 블의 폴더에 넣은 커스텀 이미지입니다. 300x300 png 이미지입니다.

# 295055은 진한 파란색입니다.

내 코드는 배경과 텍스트 색상을 변경합니다. 버튼 텍스트의 경우 Google의 기본 stlyes (위에서 제공 한 링크)를 살펴보십시오.

그런 다음 Android Manifest에 코드를 포함해야합니다.

<application
android:theme="@style/Theme.AppBaseTheme">

You can't use @android:style/TextAppearance as the parent for the whole app's theme; that's why koopaking3's solution seems quite broken.

To change default text colour everywhere in your app using a custom theme, try something like this. Works at least on Android 4.0+ (API level 14+).

res/values/themes.xml:

<resources>    
    <style name="MyAppTheme" parent="android:Theme.Holo.Light">
        <!-- Change default text colour from dark grey to black -->
        <item name="android:textColor">@android:color/black</item>
    </style>
</resources>

Manifest:

<application
    ...
    android:theme="@style/MyAppTheme">

Update

A shortcoming with the above is that also disabled Action Bar overflow menu items use the default colour, instead of being greyed out. (Of course, if you don't use disabled menu items anywhere in your app, this may not matter.)

As I learned by asking this question, a better way is to define the colour using a drawable:

<item name="android:textColor">@drawable/default_text_color</item>

...with res/drawable/default_text_color.xml specifying separate state_enabled="false" colour:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="@android:color/darker_gray"/>
    <item android:color="@android:color/black"/>
</selector>

Check if your activity layout overrides the theme, look for your activity layout located at layout/*your_activity*.xml and look for TextView that contains android:textColor="(some hex code") something like that on activity layout, and remove it. Then run your code again.


<color name="white">#FFFFFF</color>

<style name="Mytext" parent="@android:style/TextAppearance.Medium"> 
    <item name="android:textSize">20sp</item> 
    <item name="android:textColor">@color/white</item> 
    <item name="android:textStyle">bold</item>
    <item name="android:typeface">sans</item>
</style>

try this one ...

ReferenceURL : https://stackoverflow.com/questions/9593133/how-to-change-default-text-color-using-custom-theme

반응형