在线性布局中将七个按钮并排放置
Place seven buttons next to each other in linearlayout
我有一个 linearlayout
,里面有七个按钮。但是,按钮不显示其文本,因为它们的放置方式错误。这就是为什么我将每个按钮的 weightsum
设置为 7 并将 weight
设置为 1 的原因。它部分起作用了,因为我在虚拟设备中得到了预期的结果。然而,当我在 phone 上安装该应用程序时,按钮仍然相互隐藏。我该怎么办?
线性布局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
您可以尝试在 LinearLayout 中使用 TableLayout。
table 将有一行七列。
这样,您的按钮也将等距分布。
<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>
您还需要为每个按钮添加边距,以指定它们彼此之间的分隔程度。
请尝试此代码..希望这有效
<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
参数设置,因此它无法在有限的 space 内呈现文本。如果您希望看到文本,则需要减小文本大小。如果您需要自动缩放文本大小,请查看此 link.
换句话说,您可能希望为低分辨率设备制作两行按钮。或者根据您要实现的目标找到按钮的替代品。
我有一个 linearlayout
,里面有七个按钮。但是,按钮不显示其文本,因为它们的放置方式错误。这就是为什么我将每个按钮的 weightsum
设置为 7 并将 weight
设置为 1 的原因。它部分起作用了,因为我在虚拟设备中得到了预期的结果。然而,当我在 phone 上安装该应用程序时,按钮仍然相互隐藏。我该怎么办?
线性布局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
您可以尝试在 LinearLayout 中使用 TableLayout。 table 将有一行七列。 这样,您的按钮也将等距分布。
<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>
您还需要为每个按钮添加边距,以指定它们彼此之间的分隔程度。
请尝试此代码..希望这有效
<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
参数设置,因此它无法在有限的 space 内呈现文本。如果您希望看到文本,则需要减小文本大小。如果您需要自动缩放文本大小,请查看此 link.
换句话说,您可能希望为低分辨率设备制作两行按钮。或者根据您要实现的目标找到按钮的替代品。