如何使动态创建的视图垂直排列?

时间:2016-05-14 13:37:47

标签: android android-linearlayout

我有一个LinearLayout。按钮"去"添加了XML:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView21"
    android:layout_marginTop="10dp"
    android:background="#0099FF"
    android:text="go"
    android:textColor="#FFFFFF" />

动态复选框:

for (final String str : strs) {
    CheckBox cb = new CheckBox(getApplicationContext());
    cb.setText(str);
    cb.setTextColor(Color.BLACK);
    thisll.addView(cb);
}

目前元素是水平排列的: enter image description here

如何使它们垂直排列?

1 个答案:

答案 0 :(得分:2)

您是否尝试将orientation="vertical"添加到LinearLayout?

例如

<LinearLayout
    android:layout_height="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!-- The things you want aligned vertically go here -->
</LinearLayout>
相关问题