如何在RelativeLayout

时间:2017-09-17 23:26:20

标签: java android android-preferences android-relativelayout

动态创建布局后,我想根据PreferenceFragment中的切换开关位置显示/隐藏文本视图,但它不起作用:

public class MyFragment extends android.support.v4.app.Fragment {

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;

    boolean diagramState;

    public MyFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.my_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        View v = getView();
        assert v != null;

        RelativeLayout relativelayout = v.findViewById(myFragmentlayout);

        RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(GridLayout.LayoutParams.MATCH_PARENT, GridLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(GridLayout.LayoutParams.WRAP_CONTENT, GridLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams rlp3 = new RelativeLayout.LayoutParams(GridLayout.LayoutParams.WRAP_CONTENT, GridLayout.LayoutParams.WRAP_CONTENT);


        // add text views
        TextView tv1 = new TextView(getActivity());
        tv1.setText(getString(R.string.blue));
        tv1.setTextColor(Color.WHITE);

        TextView tv2 = new TextView(getActivity());
        tv2.setText(getString(R.string.green));
        tv2.setTextColor(Color.WHITE);

        TextView tv3 = new TextView(getActivity());
        tv3.setText(getString(R.string.orange));
        tv3.setTextColor(Color.WHITE);


        tv1.setId(View.generateViewId());
        tv2.setId(View.generateViewId());
        tv3.setId(View.generateViewId());

        rlp2.addRule(RelativeLayout.BELOW, tv1.getId());
        rlp3.addRule(RelativeLayout.BELOW, tv2.getId());

        tv1.setLayoutParams(rlp1);
        tv2.setLayoutParams(rlp2);
        tv3.setLayoutParams(rlp3);

        relativelayout.addView(tv1);
        relativelayout.addView(tv2);
        relativelayout.addView(tv3);

        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume(){
        super.onResume();
        loadPreferences();
        displaySettings(getView());
    }

    private void loadPreferences(){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
        diagramState = sharedPreferences.getBoolean("pref_pref1", true);
    }

    public void displaySettings() {
        if (diagramState) {
            tv1.setVisibility(View.VISIBLE);
            tv2.setVisibility(View.VISIBLE);
            tv3.setVisibility(View.VISIBLE);
        } else {
            tv1.setVisibility(View.GONE);
            tv2.setVisibility(View.GONE);
            tv3.setVisibility(View.GONE);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的错误不言而喻,只需在类中全局声明TextView变量:

public class MyFragment extends android.support.v4.Fragment {

    // Part of your class

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        //Part of your code

        // add text views
        tv1 = new TextView(getActivity());
        tv1.setText(getString(R.string.blue));
        tv1.setTextColor(Color.WHITE);

        tv2 = new TextView(getActivity());
        tv2.setText(getString(R.string.green));
        tv2.setTextColor(Color.WHITE);

        tv3 = new TextView(getActivity());
        tv3.setText(getString(R.string.orange));
        tv3.setTextColor(Color.WHITE);

        // Rest of your code
    }

    // Rest of your class
}