【问题标题】:Android Studio how to make a 2 column LinearLayoutAndroid Studio 如何制作 2 列 LinearLayout
【发布时间】:2016-07-30 14:25:35
【问题描述】:

我对 Android 应用开发非常是新手,我正在尝试在 Android Studio 中实现以下按钮布局。

[

我一直在尝试使用线性布局,但无法正确使用。

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:weightSum="1">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="#016eff"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_margin="10dp"
        android:textColor="#ffffff"
        android:layout_weight="0.48" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button2"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="#016eff"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_margin="10dp"
        android:textColor="#ffffff"
        android:layout_weight="0.48" />
</LinearLayout>

这样做的问题是,如果我在线性布局中添加了另一个按钮,那么它们只会被挤压在一起,而不是将按钮添加到下一行。

谁能告诉我让我的 LinearLayout 每行只有 2 个小部件,或者提供另一个修复。

任何帮助将不胜感激谢谢:-)

【问题讨论】:

  • 尝试使用表格布局而不是线性布局
  • 好的,我现在就去试试。
  • @Vishwa 非常感谢,我设法让它工作了!
  • 我会保留这个问题,以防将来有人遇到同样的问题。
  • 恭喜。请也发布您的答案。这样可以帮助某人实现它。

标签: android xml android-studio android-linearlayout


【解决方案1】:

好的,感谢 Vishwa 的评论,我设法找到了解决方法。 但是,我实际上并没有找到让 LinearLayout 有 2 列的方法。

相反,我改为使用 TableLayout 并拉伸 0 和 1 列以占据整个屏幕。这就是我的 XML 最终的样子。 (它有额外的东西来获得我的设计)

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:stretchColumns="0,1">

    <TableRow
        android:layout_width="match_parent"
        android:paddingBottom="8dp"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Events"
            android:id="@+id/eventButton"
            android:layout_column="0"
            android:background="#016eff"
            android:layout_marginRight="8dp"
            android:textColor="#ffffff"
            android:textStyle="normal"
            android:textSize="40px" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Absentee"
            android:id="@+id/absenteeButton"
            android:layout_column="1"
            android:background="#016eff"
            android:textColor="#ffffff"
            android:textStyle="normal"
            android:textSize="40px" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:paddingBottom="8dp"
        android:layout_height="match_parent" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Contacts"
            android:id="@+id/contactsButton"
            android:layout_column="0"
            android:background="#016eff"
            android:layout_marginRight="8dp"
            android:textColor="#ffffff"
            android:textStyle="normal"
            android:textSize="40px" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Alerts"
            android:id="@+id/alertButton"
            android:layout_column="1"
            android:background="#016eff"
            android:textColor="#ffffff"
            android:textStyle="normal"
            android:textSize="40px" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:paddingBottom="8dp"
        android:layout_height="match_parent" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Links"
            android:id="@+id/linksButton"
            android:layout_column="0"
            android:background="#016eff"
            android:layout_marginRight="8dp"
            android:textColor="#ffffff"
            android:textStyle="normal"
            android:textSize="40px" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Newsletter"
            android:id="@+id/newsletterButton"
            android:layout_column="1"
            android:background="#016eff"
            android:textColor="#ffffff"
            android:textSize="40px"
            android:textStyle="normal" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:paddingBottom="8dp"
        android:layout_height="match_parent" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Kamar"
            android:id="@+id/kamarButton"
            android:layout_column="0"
            android:background="#016eff"
            android:layout_marginRight="8dp"
            android:textColor="#ffffff"
            android:textStyle="normal"
            android:textSize="40px" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="News"
            android:id="@+id/newsButton"
            android:layout_column="1"
            android:background="#016eff"
            android:textColor="#ffffff"
            android:textSize="40px"
            android:textStyle="normal" />
    </TableRow>

</TableLayout>

【讨论】:

    【解决方案2】:

    您只需在每对按钮周围放置一个带有android:orientation="horizontal" 的单独LinearLayout。那么父LinearLayout应该有android:orientation="vertical"并且weightsum应该在每个水平LinearLayout中。

    【讨论】:

    • 感谢您的帮助,一个问题有多种解决方案真是太好了。
    • 我发现使用嵌套的 LinearLayouts 比 TableLayout 更灵活,例如当您需要一排按钮以不同的方式排列,或者让一行具有不同数量的布局时。
    【解决方案3】:

    LinearLayout 非常适合您想要实现的目标。请查看 LinearLayout 对象的权重和方向属性。 Linear Layout

    你想要什么,你可以这样做:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center">
    
            <TextView
                android:text="Whatever You Want Here"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="36sp"/>
        </LinearLayout>
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
    
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center">
    
                <Button
                    android:text="Button 1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"/>
    
                <Button
                    android:text="Button 2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"/>
            </LinearLayout>
    
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center">
    
                <Button
                    android:text="Button 3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"/>
    
                <Button
                    android:text="Button 4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"/>
            </LinearLayout>
    
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center">
    
                <Button
                    android:text="Button 5"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"/>
    
                <Button
                    android:text="Button 6"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"/>
            </LinearLayout>
    
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center">
    
                <Button
                    android:text="Button 7"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"/>
    
                <Button
                    android:text="Button 8"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    

    输出:

    请注意,因为嵌套过多的权重属性可能会带来一些负面的性能问题。

    【讨论】:

      猜你喜欢
      • 2020-08-17
      • 1970-01-01
      • 2012-06-14
      • 2016-04-15
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      相关资源
      最近更新 更多