以相反的顺序在LinearLayout中添加视图

时间:2019-10-01 10:31:59

标签: java android-linearlayout

我正在将视图动态添加到线性布局。我希望视图顺序相反。像第一个一样,我添加了RadioButton,然后添加了CheckBox,所以我想让CheckBox在顶部,然后是RadioButton。在简单的新添加的视图中应显示在顶部。我没有使用Recycelerview,也没有。

我已经尝试 android:layoutDirection,但它只能在RTL或LTR上运行。

这就是我以线性布局添加它们的方式

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);

1 个答案:

答案 0 :(得分:0)

根据documentation,您可以将索引指定为第二个参数,因此下面的操作可以满足您的要求

body, html { 
  margin: 0;
  padding:0;
}

.wrapper {
  height: 100vh;
  overflow-x: hidden;
  overflow-y: auto;
  perspective: 1px;
}

.section {
  position: relative;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 48px;
  color: white;
}

.parallax::after {
  content: " ";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  transform: translateZ(-1px) scale(2);
  background-size: 100%;
  z-index: -1;
  background-image: url('http://placeimg.com/640/480/any');
}

.content {
  height: 200vh;
  display: flex;
  justify-content: center;
  background: red;
}