为什么了maxWidth上一个按钮不起作用,如何解决呢? [英] Why does maxWidth on a Button not work and how to work around it?

查看:655
本文介绍了为什么了maxWidth上一个按钮不起作用,如何解决呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对布局两个按钮和一个大屏幕设备上(片)我想他们的上限宽度,使他们不看可笑。我预计使用了maxWidth属性,但它显然无助于我的情况。这里的布局定义 - 按钮使用了布局的整个宽度,忽略了maxWidth任意值

I have two buttons on the layout, and on a large-screen device (tablets) I want to cap their width so they don't look ridiculous. I expected to use the maxWidth attribute but it apparently does nothing in my scenario. Here's the layout definition - the buttons use up the full width of the layout, ignoring any value in maxWidth.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
>
<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:maxWidth="100dp"
    android:text="Button 1"
/>
<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:maxWidth="100dp"
    android:text="Button 2"
/>
</LinearLayout>

为什么,以及如何解决这个(显然是疯了)限制?

Why, and how to workaround this (apparently crazy) limitation?

推荐答案

删除的android:layout_weight =1行从两个按钮。
结果添加安卓了minWidth =50dp的android:layout_width =WRAP_CONTENT

Remove the android:layout_weight="1" line from both buttons.
Add android:minWidth="50dp" and android:layout_width="wrap_content"

编辑:结果

您需要手动计算按钮大小取决于屏幕尺寸。首先,你需要有一个标准的屏幕尺寸集。例如,如果你开发的屏幕宽度400像素的应用程序,该按钮是100像素宽,则维持相同的按钮宽度屏幕宽度上均配方比例设备将如下:

You need to calculate the button sizes manually depending on the screen size. First you need to have a standard screen size set. For example, If the you develop the app on a screen width 400px, and the button is 100px wide, then the formula for maintaining the same button width to screen width ratio on all devices will be as follows

 DisplayMetrics metrics = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(metrics);
 buttonWidth = 100 * (metrics.widthPixels/400); 

使用情况:结果如果屏幕宽度= 480像素,那么按钮的宽度应该是120像素

Use case:
If screen width = 480px, then button width should be 120px.

这篇关于为什么了maxWidth上一个按钮不起作用,如何解决呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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