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

标签 android button imagebutton

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

我希望 ImageButton 的宽度和它的长度完全一样。

目前看起来 ImageButton 的宽度就像没有缩放时一样(ImageButton width [px] = unscaled drawable width [px]):

example

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

它应该是什么样子:

objective

最佳答案

试试这个,我认为这应该可行:

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

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

关于android - ImageButton:强制方形图标(高度=WRAP_CONTENT,宽度=?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9852568/

相关文章:

android - EditText 给出奇怪的字符串输出

android - 关闭 OnClickListener 然后再打开

css - 我的 CSS3 没有在我的按钮上淡出

c# - 如何在 ASP.NET 中打开从 Iframe 重定向的页面以在父窗口中打开?

Android imagebutton 以编程方式更改?

swift - ios12系统图像按钮的色调颜色不起作用

android - 平板电脑和服务器之间的双向实时通信系统

android - 手机间隙 : Android/iOS is not showing live stream video using jwplayer

android - React Native 构建失败 Could not HEAD/android-jsc/maven-metadata.xml'

Jquery 按钮 CSS - 填充和边距