在我的水平 LinearLayout 中,我有一个 TextEdit 和一个 ImageButton。 ImageButton 与 TextEdit 一样高。
我希望 ImageButton 的宽度和它的长度完全一样。
目前看起来 ImageButton 的宽度就像没有缩放时一样(ImageButton width [px] = unscaled drawable width [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>
它应该是什么样子:
最佳答案
试试这个,我认为这应该可行:
<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/