Android中是否存在将内部控件向下排列而非横向延伸的布局方向?
Android LinearLayout纵向排列实现方案
现有LinearLayout默认展示效果为横向排列,如下图:
需要实现内部控件从上到下纵向排列的效果,预期如下图:
解决方案
LinearLayout的android:orientation属性控制内部子控件的排列方向,该属性默认值为horizontal(横向排列),你只需要在布局文件的LinearLayout标签中显式将该属性设置为vertical即可:
<!-- 核心修改点:添加android:orientation="vertical"属性 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="控件1" android:layout_margin="8dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="控件2" android:layout_margin="8dp"/> <!-- 其余子控件都会自动按纵向顺序排列 --> </LinearLayout>
内容的提问来源于stack exchange,提问作者Ayaz Alvi
相关产品推荐
相关产品推荐

