Android 在线性布局中添加视图

Android 在线性布局中添加视图,android,android-layout,android-widget,Android,Android Layout,Android Widget,大家好,我这里有一个问题,我有一个线性布局,我想添加按钮在水平,但我也需要它自动断开线,如果按钮触摸线性布局边缘。 我编码了我的按钮(不是XML)并将其设置为“wrapcontent”(内部文本)。我的英语不是很好,所以我在这里画了一幅图 谢谢你的阅读 如果您以前知道按钮的大小,则可以将布局放入布局中。请参见此代表您的图像的示例: <LinearLayout android:id="@+id/outerLayout" android:layout_width="match_

大家好,我这里有一个问题,我有一个线性布局,我想添加按钮在水平,但我也需要它自动断开线,如果按钮触摸线性布局边缘。 我编码了我的按钮(不是XML)并将其设置为“wrapcontent”(内部文本)。我的英语不是很好,所以我在这里画了一幅图


谢谢你的阅读

如果您以前知道按钮的大小,则可以将布局放入布局中。请参见此代表您的图像的示例:

<LinearLayout
    android:id="@+id/outerLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <LinearLayout 
        android:id="@+id/innerLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button 
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="wrap_content"
            /> 
        <Button 
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="wrap_content"
            /> 
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/innerLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button 
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="wrap_content"
            /> 
        <Button 
            android:id="@+id/button4"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="wrap_content"
            /> 
    </LinearLayout>
    <Button
       android:id="@+id/button5"
       android:layout_width="match_parent"
       android:layout_weight="5"
       android:layout_height="wrap_content"
       /> 
</LinearLayout>

如您所见,使用
layout\u weight
属性可以操纵按钮的宽度。这也可以通过在
layout\u width
中设置具体尺寸来实现,但
match\u parent
除外


如果从代码中添加
视图
s,也会出现同样的情况。然而,我更喜欢用XML解释Android布局,因为你有一个更好的概览。

@ValentinoRu,我按代码制作一个按钮,设置文本并包装内容……你可以使用
RelativeLayout
,在运行时获取窗口宽度,并根据按钮宽度和屏幕宽度与相对位置对齐。您要查找的称为
FlowLayout
,它不直接在框架中。然而,Android团队的Romain Guy创建了一个示例实现,包括一个视频,他在视频中实时编码并解释了示例。你可以从他的回答中看出这一点:@Devunwired:你让我过了一天。非常感谢你。。