我想在点击LinearLayout时更改我的应用程序的样式

时间:2017-12-11 15:57:10

标签: android android-fragments android-styles

我想让用户有机会改变他们喜欢的颜色。换句话说,目的是改变(colorPrimary,colorPrimaryDark,colorAccent),我在参数片段中添加了一些LinearLayouts,但是甚至在我的中添加了“setTheme” LinearLayout点击没有发生任何事情的代码..如果我的代码中有错误,请将解决方案留在评论中。

this how the PrametresFragment is looks like

我的代码:

ParametresFragment:

public class ParametresFragment extends Fragment {

    LinearLayout greenStyle;

    public ParametresFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment


        View v = inflater.inflate(R.layout.fragment_parametres, container, false);



        greenStyle = (LinearLayout) v.findViewById(R.id.greenStyle);
        greenStyle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().setTheme(R.style.AppTheme_Green);
                Toast.makeText(getActivity().getApplicationContext(),"clicked",Toast.LENGTH_SHORT).show();
            }
        });


        return v;

    }

}

styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.Green" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">#6ed75c</item>
        <item name="colorPrimaryDark">#44ab32</item>
        <item name="colorAccent">#fff</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.nobar" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>


    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

0 个答案:

没有答案