You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中是否存在将内部控件向下排列而非横向延伸的布局方向?

Android LinearLayout纵向排列实现方案

现有LinearLayout默认展示效果为横向排列,如下图:
现有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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 00:24:01