2017-12-05 1 views
1

조각에서 전체 테마의 테마를 어두운 테마로 변경할 수 있습니까? 아이디어는 어두운/가벼운 테마를 교환하기 위해 전환 할 수있는 스위치가 있다는 것입니다. 이것은 조각 내에서 모두 완료되었으므로 조금 혼란스러워지기 시작합니다. 여기Android Studio의 조각에서 전체 앱 테마 변경

Switch switchTheme = (Switch) view.findViewById(R.id.switchTheme); 

    switchTheme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     // switchDark(getView(), isChecked); 
      TextView txt = (TextView) getView().findViewById(R.id.lblDark); 
      if (isChecked) { 
       txt.setText("checked"); 
       // switchDark(getView(), isChecked); 
       (MainActivity) getActivity().getTheme().applyStyle(R.style.AppThemeDark, true); 
      } 
      else{ 
       txt.setText("unchecked"); 
      } 
     } 
    }); 

내 styles.xml입니다 :

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 
<!-- colors for the dark theme --> 
<style name="AppThemeDark" parent="Theme.AppCompat"> 
    <item name="colorPrimary">@color/darkColorPrimary</item> 
    <item name="colorPrimaryDark">@color/darkColorPrimaryDark</item> 
    <item name="colorAccent">@color/darkColorAccent</item> 
</style> 
<style name="AppThemeDark.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 
<style name="AppTheme.Dark.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 
<style name="AppTheme.Dark.PopupOverlay" parent="ThemeOverlay.AppCompat" /> 

그리고 여기 내 색상입니다 전혀 여기에

> 가능한이는 청취자입니다. xxx

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="colorPrimary">#3F51B5</color> 
    <color name="colorPrimaryDark">#303F9F</color> 
    <color name="colorAccent">#FF4081</color> 
    <color name="colorGrey">#D3D3D3</color> 
    <color name="colorTransparent">#FFFFFF</color> 
    <color name="colorLearned">#32CD32</color> 
    <!-- app/src/main/res/values/colors.xml --> 
    <color name="darkColorPrimary">#1e2756</color> 
    <color name="darkColorPrimaryDark">#141831</color> 
    <color name="darkColorAccent">#2aac4b</color> 
</resources> 

더 쉬운 방법이 있나요? styles.xml 및 color.xml에 코드를 통해 액세스하고 수동으로 색상을 직접 변경할 수있는 방법이 있습니까? 예를 들어 문자 그대로 문자 그대로 getResources().getColor(R.color.colorAccent) = "FFFFFF";과 같은 작업을 수행 할 수 있습니까?

public void ToggleTheme(boolean isChecked){ 
    if (isChecked) { 
     this.getTheme().applyStyle(R.style.AppThemeDark_NoActionBar, true); 
    } 
    else{ 
     this.getTheme().applyStyle(R.style.AppTheme, true); 
    } 
} 

그것은 다음과 같은처럼 조각 내에서 호출된다 :

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_settings, container, false); 


    Switch switchTheme = (Switch) view.findViewById(R.id.switchTheme); 

    switchTheme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     // switchDark(getView(), isChecked); 
      TextView txt = (TextView) getView().findViewById(R.id.lblDark); 
      if (isChecked) { 
       txt.setText("checked"); 
       // switchDark(getView(), isChecked); 
       // getResources().getColor(R.color.colorPrimary); 
       ((MainActivity) getActivity()).ToggleTheme(isChecked); 
      } 
      else{ 
       txt.setText("unchecked"); 
       ((MainActivity) getActivity()).ToggleTheme(isChecked); 
      } 
     } 
    }); 

    return view; 
} 

나는이되는 주요 활동, 내부 함수를 작성 지금 무슨 짓을

업데이트 테마를 변경하는 것처럼 보이지만 배경을 흰색으로 유지합니다. 나는 점검했고 나의 색은 실제로 적용 되어야만하는 더 어두운 색으로 설정되었다.

+0

당신이해야 두 개의 테마 (MainActivity) getActivity() .setTheme (R.style.DarkThemeHere); –

+0

나는 내 질문을 지금 가지고있는 것으로 업데이트 할 것이다. – Matt

답변

0

이 방법은 스타일을 정의하고 스타일/색상을 수동으로 설정하는 대신 스타일 이름을 설정하여 테마를 전환하는 가장 좋은 방법이라고 생각합니다.

그리고 흰색로 배경을 설정, 변경하려는 스타일의 내부에이 코드를 추가하려고 :

<item name="android:windowBackground">@color/background</item> 

참조 : Android - how to set background color of all screens?

관련 문제