2013-08-31 39 views
2

我一直在寻找这个答案,但我找不到适合自己的一个。 我想将三张图片放在线性布局的同一行中。我想他们是ImageButtons,这里是我的代码:如何调整ImageButton的宽度和高度?

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <ImageButton 
     android:id="@+id/ImageButton05" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:scaleType="centerInside" 
     android:src="@drawable/background" /> 

    <ImageButton 
     android:id="@+id/ImageButton04" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:scaleType="centerInside" 
     android:src="@drawable/background" /> 

    <ImageButton 
     android:id="@+id/ImageButton03" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:scaleType="centerInside" 
     android:src="@drawable/background" /> 

</LinearLayout> 

我得到的问题是,作为画面的背景“太大,则显示该按钮的重权而高度是大了很多超出预期。 它看起来像这样:

looks http://imageshack.us/a/img18/1516/x3qi.png

它应该是这样的:

should look http://imageshack.us/a/img855/834/403t.png

我该如何解决,如果没有在指定的DP的大小?

+0

无需为标题添加标签(例如,android)。这就是标签的用途。 – ChiefTwoPencils

+0

@BobbyDigital好的,对不起,我下次不会这样做。 – Monica

回答

7

只需添加android:adjustViewBounds="true"每个你的ImageButtons。这就是你需要的。

+0

耶,谢谢,那工作! – Monica

0

补充一点:

android:adjustViewBounds="true" 

ImageButton

,并为imageButton

android:maxHeight = "yourValue" 
1

maxHeightActivity你可以尝试的最大高度设置为ImageButton的宽度,如下所示:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    ImageButton ib = (ImageButton) findViewById(R.id.myImageButton); 
    ib.setMaxHeight(ib.getWidth()); 

    //the rest of your onCreate method... 
} 
0

以garvity =“top”取垂直方向的父线性布局,并为子线性布局提供重量。

0

只需将dimens包含到您的xml中并将其设置在layout_width或layout_height属性上即可。

(dimens.xml) 
<resources> 
    <dimen name="button_height">50dp</dimen> 
    <dimen name="button_width">25dp</dimen> 
</resources> 

(your layout.xml) 
<ImageButton 
     android:id="@+id/imageButton" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:background="@drawable/stylebuttontl" 
     android:layout_width="@dimen/button_width" 
     android:layout_height="@dimen/button_height" /> 
相关问题