I want to change the style of my app when a LinearLayout clicked

问题内容:

I want to give users the chance to change their favorite color, I have added some LinearLayouts in Parameters fragment, but even adding “setTheme” in my code when LinearLayout clicked nothing happening .. please if there is a mistake in my code leave the solution in a comment .

My code :

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>

问题评论:

    
Do you see the Toast? Why are you implementing this as a click on a LinearLayout rather than with a Button some other more appropriate View?

答案:

答案1:

Put getActivity().recreate() after getActivity().setTheme(R.style.AppTheme_Green).

答案评论:

答案2:

Make sure your LinearLayout is clickable and that any child view in it is not taking the touch and saving then your layout to receive the event.

So make sure to set the property android:clickable to true for the LinearLayout.

And set that same property android:clickable to false for the children of the layout.

答案评论:

原文地址:

https://stackoverflow.com/questions/47756672/i-want-to-change-the-style-of-my-app-when-a-linearlayout-clicked

添加评论