【问题标题】:Place seven buttons next to each other in linearlayout在线性布局中将七个按钮彼此相邻放置
【发布时间】:2016-11-05 20:35:33
【问题描述】:

我有一个linearlayout,里面有七个按钮。但是,这些按钮不显示其文本,因为它们的放置方式错误。这就是为什么我将每个按钮的 weightsum 设置为 7 并将 weight 设置为 1。它部分有效,因为我在虚拟设备中得到了预期的结果。然而,当我在手机上安装该应用程序时,按钮仍然相互隐藏。我该怎么办?

线性布局xml:

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:weightSum="7"
            android:id="@+id/linearLayout"
android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" android:baselineAligned="false">
        <Button style="?android:attr/buttonStyleSmall" android:layout_width="0dp" android:layout_height="wrap_content"
                android:text="MON" android:id="@+id/mon" android:checked="false"
                android:singleLine="true" android:layout_weight="1"/>
        <Button style="?android:attr/buttonStyleSmall" android:layout_width="0dp" android:layout_height="wrap_content"
                android:text="TUE" android:id="@+id/tue" android:checked="false"
                android:singleLine="true" android:layout_weight="1"/>
        <Button style="?android:attr/buttonStyleSmall" android:layout_width="0dp" android:layout_height="wrap_content"
                android:text="WED" android:id="@+id/wed" android:checked="false"
                android:singleLine="true" android:layout_weight="1"/>
        <Button style="?android:attr/buttonStyleSmall" android:layout_width="0dp" android:layout_height="wrap_content"
                android:text="THU" android:id="@+id/thu" android:checked="false"
                android:singleLine="true" android:layout_weight="1"/>
        <Button style="?android:attr/buttonStyleSmall" android:layout_width="0dp"
                android:layout_height="wrap_content" android:text="FRI" android:id="@+id/fri"
                android:checked="false" android:singleLine="true" android:layout_weight="1"/>
        <Button style="?android:attr/buttonStyleSmall" android:layout_width="0dp"
                android:layout_height="wrap_content" android:text="SAT" android:id="@+id/sat"
                android:checked="false" android:singleLine="true" android:layout_weight="1"/>
        <Button style="?android:attr/buttonStyleSmall" android:layout_width="0dp"
                android:layout_height="wrap_content" android:text="SUN" android:id="@+id/sun"
                android:checked="false" android:singleLine="true" android:layout_weight="1"/>
    </LinearLayout>

虚拟设备分辨率(我得到了预期的结果):768x1280

真实设备分辨率(我有问题):480x854

截图: This is a screenshot of the result from the final accepted answer

【问题讨论】:

  • 请粘贴您的xml代码
  • 发布您的 xml 布局,如果可能的话,请发布您尝试过的手机的分辨率。
  • 截图也可能有帮助。
  • 网站也存在同样的问题:您必须找到适用于所有设备的设计。您是否像真实设备一样设置了虚拟设备的显示尺寸?
  • @Oasa 我添加了 xml 和维度。

标签: android android-linearlayout android-layout-weight


【解决方案1】:

请尝试此代码.. 希望这有效

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="7">
            <Button
                android:id="@+id/bt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MON"
                android:weight="1"/>

            <Button
                android:id="@+id/bt2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TUE"
                android:weight="1"/>
           <Button
                android:id="@+id/bt3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="WED"
                android:weight="1"/>
           <Button
                android:id="@+id/bt4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="THU"
                android:weight="1"/>
            <Button
                android:id="@+id/bt5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="FRI"
                android:weight="1"/>
           <Button
                android:id="@+id/bt6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="SAT"
                android:weight="1"/>
           <Button
                android:id="@+id/bt7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="SUN"
                android:weight="1"/>

  </LinearLayout>

已更新让我知道它是否有效

    <?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="7">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <Button
                android:id="@+id/bt1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="MON"
                android:weight="1"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <Button
                android:id="@+id/bt2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TUE"
                android:weight="1"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <Button
                android:id="@+id/bt3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="WED"
                android:weight="1"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <Button
                android:id="@+id/bt4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="THU"
                android:weight="1"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <Button
                android:id="@+id/bt5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="FRI"
                android:weight="1"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <Button
                android:id="@+id/bt6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="SAT"
                android:weight="1"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <Button
                android:id="@+id/bt7"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="SUN"
                android:weight="1"/>

        </LinearLayout>  

    </LinearLayout>

</LinearLayout>

【讨论】:

  • 如您所见,他正在尝试使用 layout_weight,以便响应式。将宽度设为wrap_content 肯定会将按钮显示为完整,但不是作者希望的方式。此外,我认为大多数按钮对于低分辨率设备甚至都不可见。
  • @vishnumm93 它没有用。按钮定位正常,但没有显示它们的文本。
  • 谢谢它的工作,但我还必须按照@Oasa 在他的回答中建议的那样减小文本大小,并使按钮“更高”以获得结果。我会尝试发布截图在我的问题与结果。
  • @vishnumm93 我不知道你为什么要使用layout_weight 而不是将0dp 放在高度/宽度的任何位置以及不必要的布局嵌套。
  • @Oasa 在这里我们不需要考虑按钮的高度,为什么我忽略了布局高度为 0dp。我们使用匹配父级作为宽度,因为我们需要将所有按钮包含在父级线性布局中。
【解决方案2】:

您可以尝试在 LinearLayout 中使用 TableLayout。 该表将有一行,但有七列。 这样,您将拥有同样间隔的按钮。

 <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="7">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/bt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt1"/>

            <Button
              android:id="@+id/bt2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt2"/>
           <Button
              android:id="@+id/bt3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt3"/>
           <Button
              android:id="@+id/bt4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt4"/>
            <Button
              android:id="@+id/bt5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt5"/>
           <Button
              android:id="@+id/bt6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt6"/>
           <Button
              android:id="@+id/bt7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt7"/>

        </TableRow>
  </TableLayout>

您还需要为每个按钮添加边距,以指定它们彼此之间的分隔程度。

【讨论】:

  • 我怀疑这是否可行,因为每个视图的宽度都设置为wrap_content
【解决方案3】:

随着屏幕分辨率的降低,按钮无法适应正常大小的文本。由于宽度由layout_weight 参数设置,它无法在有限的空间内呈现文本。如果您希望查看文本,则需要减小文本大小。如果您需要自动缩放文本大小,请查看link

另外,您可能希望为低分辨率设备设置两行按钮。或者根据您要实现的目标找到按钮的替代方案。

【讨论】:

    猜你喜欢
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    相关资源
    最近更新 更多