简体   繁体   中英

Android Align Button Dynamically In RelativeLayout In Fragment

I am trying to align the button vertically that are generated dynamically in a for loop within a RelativeLayout fragment, but the buttons are overlapping at the same position, I have looked through alot of the other posts and I still don't know how to fix it, helpppppp

I know use linearLayout will solve the problem, but the buttons are the wrong size and position, and for some reason I can't add the linearLayout to the relativeLayout. I have pasted 2 code sample, and I need a solution for 1 of them. Thanks in advance.

Below is my code (Attempt 1: Align button in relative layout):

View view = inflater.inflate(R.layout.home, container, false);

RelativeLayout relativeLayout = new RelativeLayout(getActivity().getApplicationContext());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

List<String> test_list = Arrays.asList("sup1", "sup2", "sup3");

for (int i = 0; i < test_list.size(); i++) {
    final String test_name = test_list.get(i);
    Button button = new Button(getActivity());
    button.setText("Test" + test_name + " " + i);
    button.setId(i);

    if(i > 0){
        lp.addRule(RelativeLayout.RIGHT_OF, (i-1));
    }

    relativeLayout.addView(button, lp);

    button.setOnClickListener(new View.OnClickListener() {@Override 
        public void onClick(View v) {
            FragmentManager fragmentmanager = getFragmentManager();
            FragmentTransaction transaction = fragmentmanager.beginTransaction(); // allows attach,detach,etc
            transaction.replace(R.id.DB2Home, newFragment, "ChooseCategory").addToBackStack(null).commit();
        }
    });
}

((ViewGroup) view).addView(relativeLayout);

return view;

(Attempt 2: Align button in linear layout and add to relative layout)

View view = inflater.inflate(R.layout.home, container, false);

LinearLayout linearLayout = new LinearLayout(getActivity().getApplicationContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);

List<String> test_list = Arrays.asList("sup1", "sup2", "sup3");
for (int i = 0; i < test_list.size(); i++) {
    final String test_name = test_list.get(i);
    Button button = new Button(getActivity());
    button.setText(test_name);
    button.setId(i);

    linearLayout.addView(button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fragmentmanager = getFragmentManager();
            FragmentTransaction transaction = fragmentmanager.beginTransaction(); // allows attach,detach,etc
            transaction.replace(R.id.DB2Home, newFragment,"ChooseCategory").addToBackStack(null).commit();
        }
    });
}
// Doesn't work for relative layout
// RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.homescreen);
//relativeLayout.addView(linearLayout);
//((ViewGroup) view).addView(relativeLayout);

((ViewGroup) view).addView(linearLayout);

return view;

You can get to it via code:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);

button.setLayoutParams(params); //causes layout update

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM