我该如何调整ImageButton的宽度和高度?

3

我一直在寻找这个问题的答案,但是我找不到适合我的答案。 我想在线性布局中将三张图片放在同一行。我希望它们是ImageButtons,以下是我的代码:

<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>

我遇到的问题是,由于图片“background”太大,所以按钮的宽度正确显示,而高度比预期要大得多。它看起来像这样:

看起来 http://imageshack.us/a/img18/1516/x3qi.png

而它应该像这样:

应该看起来 http://imageshack.us/a/img855/834/403t.png

我如何解决这个问题,而不需要在dp中指定大小?

不需要在标题中添加标签(例如android)。这就是标签的作用。 - ChiefTwoPencils
@BobbyDigital 好的,抱歉,下次我不会这样做了。 - Monica
5个回答

8

只需在每个 ImageButton 中添加 android:adjustViewBounds="true" 即可。这就是你需要的全部。


1

加入这个:

   android:adjustViewBounds="true"

转换为 ImageButton

并为 imageButton 设置一个 maxHeight

  android:maxHeight = "yourValue"

1
在您的Activity中,您可以尝试将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
只需在xml中添加dimens并将其设置为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" />

0

在垂直方向上采用gravity="top"的父线性布局,并为子线性布局提供权重。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接