2017-01-24 1 views
0

내 Android 앱에는 두 가지 테마 (밝은 부분과 어두운 부분)가 있습니다. 이 적용됩니다모든보기가 아닌 다른 테마 스타일

<item name="android:textViewStyle">@style/TextViewDark</item> 

<style name="TextViewDark"> 
     <item name="android:textColor">?android:attr/colorAccent</item> 
</style> 

그러나 예를 들면 다음과 같습니다 :

<style name="AppThemeDark" parent="Theme.AppCompat"> 
     <item name="colorPrimary">@android:color/black</item> 
     <item name="colorPrimaryDark">@android:color/black</item> 
     <item name="colorAccent">@android:color/holo_red_dark</item> 
     <item name="android:textColor">@android:color/white</item> 
     <item name="windowActionModeOverlay">true</item> 
</style> 

<style name="AppThemeLight" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="windowActionModeOverlay">true</item> 
</style> 

따라서는, 지금은 텍스트 뷰, 예를 들어, 다른 텍스트 색상을 적용 할 수 있습니다 (어두운 테마 흰색과 검은 색 빛) 모두 TextViews.

빛 테마 : TextViews 텍스트 색상의 절반은 검정, 다른 절반 녹색

주요 질문은, 가능 다음 (안 프로그래밍) XML로 만드는 것입니다. 검은 색 테마 : 밝은 테마에서 검정색을 나타내는 텍스트보기 - 빨간색과 다른 절반 파란색 (밝은 테마에서는 초록색)입니다.

답변

0

AppThemes에서 스타일을 정의 할 필요하지는 main.xml

<LinearLayout> 

    <TextView 
     android:id="@+id/light_text_view" 
     android:text"i´m use light theme" 
     style="@style/TextViewLight"/> 

    <TextView 
     android:id="@+id/dark_text_view" 
     android:text"i´m use darktheme" 
     style="@style/TextViewDark"/> 

</LinearLayout> 

에서 사용할 수 있습니다 TextView

public class OneTextView extends TextView { 

    public OneTextView(Context context) { 
     super(context); 
     init(context); 
    } 

    public OneTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
    } 

    public OneTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context); 
    } 

    private void init(Context context){ 
     int[] attrs = new int[] { R.attr.myFirstColor}; 
     TypedArray ta = context.obtainStyledAttributes(attrs); 
     int appColor = ta.getColor(0, 0); 
     ta.recycle(); 

     // set theme color 
     setTextColor(appColor); 
    } 
} 

public class SecondTextView extends TextView { 

    public SecondTextView(Context context) { 
     super(context); 
     init(context); 
    } 

    public SecondTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
    } 

    public SecondTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context); 
    } 

    private void init(Context context){ 
     int[] attrs = new int[] { R.attr.mySecondColor}; 
     TypedArray ta = context.obtainStyledAttributes(attrs); 
     int appColor = ta.getColor(0, 0); 
     ta.recycle(); 

     // set theme color 
     setTextColor(appColor); 
    } 
} 

는 검은 색과 붉은 색

SecondTextView이 values

attr.xml을 정의, 녹색, 청색

에게

을 가질 수

을 가질 수 있습니다이

<com.route.to.class.OneTextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

OneTextView처럼 XML에서 사용할 수있는 각 클래스

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <attr name="myFirstColor" format="color" /> 
    <attr name="mySecondColor" format="color" /> 
</resources> 

그런 다음 styles.xml에서 각 테마의 색상을 정의하십시오.

<style name="Theme.MyApp" parent="@style/Theme.Light"> 
    <item name="myFirstColor">@color/black</item> 
    <item name="mySecondColor">@color/green</item> 
</style> 

<style name="Theme.MyApp.Dark" parent="@style/Theme.Dark"> 
    <item name="myFirstColor">@color/green</item> 
    <item name="mySecondColor">@color/blue</item> 
</style> 
+0

고마워요! 나는 그것의 자신의 속성을 만드는 것이 가능하다는 것을 몰랐다. 이것은 내가 찾고 있었던 바로 그 것이다. – kara4k

0

당신이 styles.xml

<style name="TextViewDark"> 
    <item name="android:textColor">?android:attr/colorAccent</item> 
</style> 

<style name="TextViewLight"> 
    <item name="android:textColor">@color/green</item> 
</style> 

에서 정의한 다음, 당신은 당신이 확장이 개 클래스를 생성

+0

아직 완전히 이해되지 않았습니다. 좀 더 자세하게 설명해 주시겠습니까? – kara4k

+0

하지만,이 경우, 어플에서 어둠으로 내 앱 테마를 전환 할 때 TextView의 스타일은 여전히 ​​동일합니다. 첫 번째 TV는 색상 악센트와 두 번째 녹색 색상을 갖습니다. 그리고 그들은 내가 선택한 주제 (밝거나 어두움)에 의존하지 않을 것입니다. – kara4k

관련 문제