简体   繁体   中英

Adding views in LinearLayout in reverse order

I am dynamically adding views to a linear layout. I want the view to be in reverse order. Like first I added RadioButton and then I added CheckBox so I want CheckBox on top then RadioButton. In simple newly added view should be shown on top. I'm not using Recycelerview nor I can.

I have tried android:layoutDirection but it only working from RTL or LTR.

This is how I'm adding them in linear layout

CardView cardView = new CardView(this);
LinearLayout.LayoutParams cardParams = new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.WRAP_CONTENT);
cardParams.setMargins(10, 10, 10, 10);
cardView.setLayoutParams(cardParams);
LinearLayout cardLayout = new LinearLayout(this);
cardLayout.setOrientation(LinearLayout.VERTICAL);

cardView.addView(cardLayout);
linearLayout.addView(cardView);

final EditText questionText = new EditText(this);
LinearLayout.LayoutParams textParams = new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.WRAP_CONTENT);
textParams.setMargins(20, 10, 20, 10);
questionText.setPadding(15, 10, 10, 10);
questionText.setText(title);

edtTags.add(questionText);
questionText.setTag(edtTags.size());
questionText.setLayoutParams(textParams);
cardLayout.addView(questionText);

As per the documentation , you can specify an index as the second parameter, so the following would do what you want

linearLayout.addView(cardView, 0);

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