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

前端之家收集整理的这篇文章主要介绍了android – ImageButton:强制方形图标(高度= WRAP_CONTENT,宽度=?)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的水平LinearLayout中,我有一个TextEdit和一个 ImageButton. ImageButton与TextEdit一样高.

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

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

  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal" >
  5.  
  6. <EditText
  7. android:id="@+id/txtName"
  8. android:layout_width="1dp"
  9. android:layout_height="wrap_content"
  10. android:layout_weight="1" />
  11.  
  12. <ImageButton
  13. android:id="@+id/btSet"
  14. android:layout_width="wrap_content"
  15. android:layout_height="match_parent"
  16. android:scaleType="fitEnd"
  17. android:src="@drawable/pin2" />
  18.  
  19. </LinearLayout>

它应该如何:

解决方法

试试这个,我认为这应该有效:
  1. <RelativeLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="40dp"
  4. android:orientation="horizontal" >
  5.  
  6. <ImageButton
  7. android:id="@+id/btSet"
  8. android:layout_width="wrap_content"
  9. android:layout_height="match_parent"
  10. android:layout_alignParentRight="true"
  11. android:scaleType="centerInside"
  12. android:adjustViewBounds="true"
  13. android:src="@drawable/pin2" />
  14.  
  15. <EditText
  16. android:id="@+id/txtName"
  17. android:layout_width="match_parent"
  18. android:layout_height="match_parent"
  19. android:layout_toLeftOf="@id/btSet" />
  20.  
  21. </RelativeLayout>

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

猜你在找的Android相关文章