怎样才能创建按钮与宽度相等? [英] How does one create Buttons with Equal Widths?

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

问题描述

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

只是增加三个按钮具有不同长度的文本标签会产生不同宽度的按钮。

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=横向
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =center_vertical
    机器人:重力=中心>
    <按钮
        机器人:ID =@ + ID / button_1
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =WRAP_CONTENT
        机器人:文本=ABCDEF/>
    <按钮
        机器人:ID =@ + ID / button_2
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =WRAP_CONTENT
        机器人:文本=GHI/>
    <按钮
        机器人:ID =@ + ID / button_3
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =WRAP_CONTENT
        机器人:文本=JKLM/>
< / LinearLayout中>  

默认按键宽度包装内容:
默认按钮的宽度包内容/

-

设置layout_weight为1,layout_width到0dip上的所有按钮,使他们平等地拉伸以填充整个屏幕宽度。我想要什么,这样的按键实在是太大了,尤其是在大屏幕上。

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=横向
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =center_vertical
    机器人:重力=中心>
    <按钮
        机器人:ID =@ + ID / button_1
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =0dip
        机器人:layout_weight =1
        机器人:文本=ABCDEF/>
    <按钮
        机器人:ID =@ + ID / button_2
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =0dip
        机器人:layout_weight =1
        机器人:文本=GHI/>
    <按钮
        机器人:ID =@ + ID / button_3
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =0dip
        机器人:layout_weight =1
        机器人:文本=JKLM/>
< / LinearLayout中>  

布局权重1按钮填充屏幕宽度:
布局重1按钮填充屏幕宽度/

-

有关weightSum父的LinearLayout设置不同的值可以用来填满整个屏幕停止按钮,但我不认为这是我要采取的路径,因为我不想让按钮占据屏幕上的大屏幕设备的很大一部分。为了澄清,使用weightSum,我可以,例如,设置三个按钮集体占据半个屏幕宽度,这可能看起来在小屏幕上还算可以,但在大屏幕上,按钮仍然会占据半个屏幕宽度,按钮,简直是比我要大得多。也许最终的解决方案将是简单地具有不同的布局文件不同的屏幕,但我宁愿不走这条路。

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=横向
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =center_vertical
    机器人:重力=中心
    机器人:weightSum =5>
    <按钮
        机器人:ID =@ + ID / button_1
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =0dip
        机器人:layout_weight =1
        机器人:文本=ABCDEF/>
    <按钮
        机器人:ID =@ + ID / button_2
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =0dip
        机器人:layout_weight =1
        机器人:文本=GHI/>
    <按钮
        机器人:ID =@ + ID / button_3
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =0dip
        机器人:layout_weight =1
        机器人:文本=JKLM/>
< / LinearLayout中>  

加权和5个小画面:
加权求和5小屏幕/

重量之和5个大型屏幕:
加权求和5大屏幕/

-

我也试过用TableLayout很多东西,但没有得到任何东西比只使用的LinearLayout更好。

GridView的是超笨拙的使用,我还没有尝试过,但。

那么,一个人如何创建具有相等的宽度,preferrably一样宽必要具有最长标签以适合按钮的内容,他们只是按钮?

任何意见是AP preciated。

(我做了搜索和发现这个问题,提出和回答了很多次,但没有我发现解决了我想要实现的答案。)

解决方案
  

也许最终的解决方案将是简单地具有不同的布局文件不同的屏幕,但我宁愿不走这条路。

许多程序员会使用 RES /布局/ RES /布局大/ 处理这样的情况。在三个按钮的有限情况下,你可能有替代品,但通常用户界面是不是想象中的那么简单。

  

那么,一个人如何创建具有相等的宽度,preferrably一样宽必要具有最长标签以适合按钮的内容,他们只是按钮?

要pferrably $ P $实现你的(原文如此)的要求,你需要创建一个自定义布局类的。 这里有一个与此相关的,仪表板模式,那你还不如一个起始点。

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天全站免登陆