【问题标题】:Android framelayouts side by sideAndroid框架布局并排
【发布时间】:2026-01-29 04:10:01
【问题描述】:

我想将两个框架布局水平并排放置。我的 layout.xml 是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.agodevs.vrcamera.MainActivity" >
<FrameLayout android:id="@+id/camera_previewl" android:layout_width="fill_parent"  android:layout_height="wrap_content"></FrameLayout>
<FrameLayout android:id="@+id/camera_previewr" android:layout_width="fill_parent" android:layout_height="wrap_content"></FrameLayout>
</RelativeLayout>

我只能水平看到一个框架布局。我做错了什么?

【问题讨论】:

    标签: java android layout android-framelayout


    【解决方案1】:

    使用这个:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    tools:context="com.agodevs.vrcamera.MainActivity" >
    
    <FrameLayout
        android:id="@+id/camera_previewl"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ff0000" >
    </FrameLayout>
    
    <FrameLayout
        android:id="@+id/camera_previewr"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#000" >
    </FrameLayout>
    
    </LinearLayout>
    

    【讨论】:

    • 打败我。虽然我个人更喜欢以总和为 1 的权重来阅读它,因此给它们中的每一个赋予 0.5 的值。实际上,这实现了相同的效果,因为没有提供 weightSum。
    • 这取决于我们如何管理体重。我一直牢记1:10的比例
    • @PiYusHGuPtA 为什么我在这里有问题?
    • 不,我没有,但有时我需要一些帮助
    • 告诉我你需要什么帮助?
    【解决方案2】:

    您有 2 个FrameLayouts,但您没有指定他们的位置。您可以在camera_previewr 上使用android:layout_toRightOf 属性并指向camera_previewl 作为锚点。但是这里不需要RelativeLayout,如果你只想水平放置2个Views,使用LinearLayout作为根容器而不是android:orientation="horizontal"

    【讨论】:

    • 注意默认方向水平的,所以不需要指定它,至少在这种情况下不需要。
    • @Daneo 是的,忘记了,水平方向确实隐式设置为水平。