2011-12-22 64 views
0

我想改变我的按钮在运行时的大小,以使他们适应屏幕好 - 因此,为了设置按钮的宽度,我做的 -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);,但它似乎并没有做任何事情,除非值是非常大的。我试着减小文字的大小或最小高度,但这似乎没有效果。

+0

澄清 - 改变按钮的宽度是否正常工作。 – Eilidh 2011-12-22 18:58:26

+0

您使用的是什么布局? – Kerry 2011-12-22 20:55:00

+0

LinearLayout,对不起。 我能够在XML文件中手动将其设置为更小,但仍然无法在运行时动态设置它。 – Eilidh 2011-12-22 21:13:02

回答

0

嗯 - 我不知道为什么setHeight工作不正常,但有更好的方法可以处理整个尺寸问题。

对于宽度,如果你想要一个按钮来填充屏幕宽度的三分之二,那么你应该在一个水平的LinearLayout中嵌套它,创建一个空白的视图,你的Button和另一个空白视图,所有fill_parent宽度和设置像这样的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

0

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

相关问题