ImageButton:强制方形图标(高度= WRAP_CONTENT,宽度=?)

Sec*_*one 8 android button imagebutton

在我的水平LinearLayout中,我有一个TextEdit和一个ImageButton.ImageButton与TextEdit一样高.

我希望ImageButton的长度和它一样宽.

目前看起来像ImageButton的宽度就像没有缩放(ImageButton width [px] =未缩放的可绘制宽度[px]):

例

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

    <EditText
        android:id="@+id/txtName"
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/btSet"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:scaleType="fitEnd"
        android:src="@drawable/pin2" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

它应该如何:

目的

Jas*_*son 21

试试这个,我认为这应该有效:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/btSet"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:scaleType="centerInside"
        android:adjustViewBounds="true"
        android:src="@drawable/pin2" />

    <EditText
        android:id="@+id/txtName"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@id/btSet" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

解释: centerInside将确保图像在比例范围内按比例缩放ImageButton.adjustViewBounds="true"将...好吧,调整视图的边界,如果图像需要缩放.


Mar*_*zon 5

尝试添加

adjustViewBounds = "真"

到ImageButton,应剪掉多余的宽度


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

    <EditText
        android:id="@+id/txtName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/btSet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/pin" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


tha*_*ksh -3

您可以使用 px 调整图像按钮的大小...如下所示...

    `android:layout_width="5px"
    android:layout_height="5px"`
Run Code Online (Sandbox Code Playgroud)