如何创建等宽的按钮? [英] How does one create Buttons with Equal Widths?

查看:17
本文介绍了如何创建等宽的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在屏幕中间显示三个按钮,并且三个按钮的宽度都相同,但它们的文本标签长度不同.

只需添加三个具有不同长度文本标签的按钮即可生成不同宽度的按钮.

<按钮android:id="@+id/button_1"android:layout_height="fill_parent"android:layout_width="wrap_content"android:text="ABCDEF"/><按钮android:id="@+id/button_2"android:layout_height="fill_parent"android:layout_width="wrap_content"android:text="GHI"/><按钮android:id="@+id/button_3"android:layout_height="fill_parent"android:layout_width="wrap_content"android:text="JKLM"/></LinearLayout>

默认按钮宽度包裹内容:

--

将所有按钮上的 layout_weight 设置为 1 并将 layout_width 设置为 0dip 会导致它们均等地拉伸以填充整个屏幕宽度.对于我想要的,这样的按钮实在是太大了,尤其是在大屏幕上.

<按钮android:id="@+id/button_1"android:layout_height="fill_parent"android:layout_width="0dip"机器人:layout_weight="1"android:text="ABCDEF"/><按钮android:id="@+id/button_2"android:layout_height="fill_parent"android:layout_width="0dip"机器人:layout_weight="1"android:text="GHI"/><按钮android:id="@+id/button_3"android:layout_height="fill_parent"android:layout_width="0dip"机器人:layout_weight="1"android:text="JKLM"/></LinearLayout>

layout weight 1 按钮填充屏幕宽度:

--

在父LinearLayout中为weightSum设置不同的值可以用来阻止按钮填满整个屏幕,但是我不认为这是我想要走的路径,因为我不想按钮占据大屏幕设备上的大部分屏幕.澄清一下,使用 weightSum,我可以,例如,将三个按钮设置为共同占据屏幕宽度的一半,这在小屏幕上可能看起来不错,但在大屏幕上,按钮仍然占据屏幕宽度的一半,而按钮只会比我想要的大得多.也许最终的解决方案是为不同的屏幕简单地使用不同的布局文件,但我宁愿不走这条路.

<按钮android:id="@+id/button_1"android:layout_height="fill_parent"android:layout_width="0dip"机器人:layout_weight="1"android:text="ABCDEF"/><按钮android:id="@+id/button_2"android:layout_height="fill_parent"android:layout_width="0dip"机器人:layout_weight="1"android:text="GHI"/><按钮android:id="@+id/button_3"android:layout_height="fill_parent"android:layout_width="0dip"机器人:layout_weight="1"android:text="JKLM"/></LinearLayout>

重和5小屏:

重和5大屏:

--

我也用 TableLayout 尝试了很多东西,但没有比使用 LinearLayout 更好的了.

GridView 使用起来非常笨拙,我还没有尝试过.

那么,如何创建具有相同宽度的按钮,最好是它们的宽度仅与具有最长标签的按钮的内容相适应?

感谢任何建议.

(我确实搜索并发现这个问题被问过和回答过很多次,但我找到的答案都没有解决我想要实现的目标.)

解决方案

也许最终的解决方案是为不同的屏幕简单地使用不同的布局文件,但我不想走这条路.

许多程序员会使用res/layout/res/layout-large/ 来处理这样的情况.在三个按钮的有限情况下,您可能有其他选择,但通常用户界面并没有那么简单.

<块引用>

那么,如何创建具有相同宽度的按钮,最好是它们的宽度仅与具有最长标签的按钮的内容相适应?

要完成您的最好"[原文如此] 要求,您需要为此创建一个自定义布局类.这里有一个与之相关的,对于仪表板模式,您可以将其用作起点.>

I want to display three buttons in the middle of a screen, and have the three buttons all be the same width, though they will have text labels of different lengths.

Just adding three buttons with text labels of different lengths produces buttons of different widths.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:gravity="center">
    <Button
        android:id="@+id/button_1"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:text="ABCDEF" />
    <Button
        android:id="@+id/button_2"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:text="GHI" />
    <Button
        android:id="@+id/button_3"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:text="JKLM" />
</LinearLayout>

default button width wraps contents:

--

Setting the layout_weight to 1 and the layout_width to 0dip on all the buttons causes them to stretch equally to fill the entire screen width. For what I want, such buttons are simply too big, especially on large screens.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:gravity="center">
    <Button
        android:id="@+id/button_1"
        android:layout_height="fill_parent"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:text="ABCDEF" />
    <Button
        android:id="@+id/button_2"
        android:layout_height="fill_parent"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:text="GHI" />
    <Button
        android:id="@+id/button_3"
        android:layout_height="fill_parent"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:text="JKLM" />
</LinearLayout>

layout weight 1 buttons fill screen width:

--

Setting different values for weightSum in the parent LinearLayout can be used to stop the buttons from filling the entire screen, but I don't think this is the path I want to take, because I don't want the buttons to occupy a large portion of the screen on large screen devices. To clarify, using weightSum, I could, for example, set the three buttons to collectively occupy half the screen width, which may look OK on small screens, but on a large screen, the buttons would still occupy half the screen width, and the buttons would simply be much larger than what I want. Perhaps the final solution will be to simply have different layout files for different screens, but I'd rather not go down this path.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:gravity="center"
    android:weightSum="5">
    <Button
        android:id="@+id/button_1"
        android:layout_height="fill_parent"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:text="ABCDEF" />
    <Button
        android:id="@+id/button_2"
        android:layout_height="fill_parent"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:text="GHI" />
    <Button
        android:id="@+id/button_3"
        android:layout_height="fill_parent"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:text="JKLM" />
</LinearLayout>

weight sum 5 small screen:

weight sum 5 large screen:

--

I also tried many things with TableLayout, but didn't get anything better than just using LinearLayout.

GridView is extra-clumsy to use, and I haven't tried it, yet.

So, how does one create buttons with equal widths, preferrably where they are only as wide as necessary to fit the contents of the button with the longest label?

Any advice is appreciated.

(I did search and find this question asked and answered many times, but none of the answers I found resolved what I'm trying to achieve.)

解决方案

Perhaps the final solution will be to simply have different layout files for different screens, but I'd rather not go down this path.

Many programmers will use res/layout/ and res/layout-large/ for handling situations like this. In the limited case of the three buttons, you might have alternatives, but usually user interfaces aren't quite that simplistic.

So, how does one create buttons with equal widths, preferrably where they are only as wide as necessary to fit the contents of the button with the longest label?

To accomplish your "preferrably" [sic] requirement, you would need to create a custom layout class for that. Here is one related to it, for the dashboard pattern, that you might use as a starting point.

这篇关于如何创建等宽的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆