2013-08-31 38 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" /> 
相關問題