2016-06-13 170 views
0

的1/3我在XML中有两个按钮是这样的:设置按钮的高度与宽度

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 





    <Button 
     android:background="@drawable/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_margin="10dp"/> 

    <Button 
     android:background="@drawable/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_margin="10dp"/> 


    </LinearLayout> 





</ScrollView> 

但是按钮会出现在广场,而我的形象的高度是宽度的1/3。 我怎样才能使按钮高度的1/3的宽度?

回答

0

为什么不使用ImageButton而不是Button,然后将高度设置为match_parent。

事情是这样的:

<ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
+0

我使用的ImageButton在第一,但同样的事情发生了.. –

+0

只有按钮不同的是,我可以设置按钮的高度与“dp”(ImageButton不能适用),但它不适合所有屏幕 –

0

试试这样说:

Button b = findViewById(R.id.button); 
b.setHeight(b.getWidth()/3); 
+0

这将返回零。你应该像这样做:http://stackoverflow.com/questions/4142090/how-to-retrieve-the-dimensions-of-a-view/4406090#4406090 – DAVIDBALAS1

+0

不..没有发生..我也创建一个按钮编程,但没有成功...它看起来正方形... –