Android中按钮的最小高度是否有硬限制?

时间:2011-12-22 18:56:44

标签: java android

我想在运行时改变按钮的大小,以使它们很好地适应屏幕 - 所以,为了设置按钮的宽度,我正在做 -

int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); 
int buttonWidth = (screenWidth*2)/3; 

然后循环浏览每个调用.setWidth(buttonWidth);的按钮。

我想以相同的方式改变高度,但是当我在模拟器上运行我的应用程序时,除非我将其设置为更大的值,否则高度不会改变。

int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); 
int buttonHeight = (screenHeight-60)/14; 

我在同一个循环中使用.setHeight(buttonHeight);,但除非值非常大,否则它似乎没有做任何事情。我已经尝试过减小文本的大小或最小高度,但这似乎没有效果。

2 个答案:

答案 0 :(得分:1)

是。根据我的经验,即使在XML中,你也不能将Button的高度降低到某个值以上,除非你还设置minHeight = 0.

答案 1 :(得分:0)

嗯 - 我不知道为什么setHeight无法正常工作,但有更好的方法可以解决整个规模问题。

对于宽度,如果你想要一个按钮填充屏幕宽度的三分之二,那么你应该将它嵌套在一个水平的LinearLayout中,创建一个空白的视图,你的按钮,以及另一个所有fill_parent的空白视图width并设置layout_weight,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical">
    <LinearLayout android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal">
            <View android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:layout_weight="2"/>
            <Button android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:text="Button"
                android:id="@+id/button"/>
            <View android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:layout_weight="2"/>
    </LinearLayout>
</LinearLayout>

就个人而言,我更喜欢上面的代码而不是修改Java代码中的布局。虽然我没有尝试过,但我相信你也可以为高度做同样的事情,但你会有一个垂直布局而且高度为fill_parent