Android 具有不同状态图像大小的ImageButton
如何防止Android 具有不同状态图像大小的ImageButton,android,android-layout,imagebutton,Android,Android Layout,Imagebutton,如何防止ImageButton具有固定大小? 选择器中使用的绘图对象具有不同的大小,我希望ImageButton调整自身大小以匹配图像大小 我尝试了adjustViewBounds,但没有成功 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width
ImageButton
具有固定大小?
选择器中使用的绘图对象具有不同的大小,我希望ImageButton
调整自身大小以匹配图像大小
我尝试了adjustViewBounds
,但没有成功
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp" >
<ImageButton
android:id="@+id/picture_imagebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/tab_background_selector" />
</RelativeLayout>
正如您可能已经知道的,在选择器中设置可绘图项并不是一种简单的方法, 我认为没有一个基于XML的解决方案可以解决您的问题。 试试这个
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/default_image"
android:contentDescription="@null"/>
</RelativeLayout>
不要使用ImageButton,只需使用图像并提供onClick方法正如其他人所提到的,最好使用
图像视图
,因为它会调整其高度。将属性setClickable
设置为像按钮一样使用。还可以将您的RelativeLayout
交换为LinearLayout
,这将使其高度与您的ImageButton
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp" >
<ImageView
android:id="@+id/picture_imagebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:setClickable="true"
android:src="@drawable/tab_background_selector" />
</LinearLayout>
或者,您可以将线性布局设置为可单击,并添加背景资源(未单击时为透明,单击时为彩色)作为选择器。尝试
android:background="@drawable/tab_background_selector"
代替
android:src="@drawable/tab_background_selector"
希望有帮助。不要使用ImageButton
,而是使用ImageView
,因为ImageButton
无法根据ImageSize
调整自身大小ImageView
是我可以为您的问题建议的最佳替代方法。通过以下方式实现:
layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image_selection"/>
</RelativeLayout>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/default_image"/> //default_image.png is displayed when the Activity loads.
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/default_image_hover" /> //default_image_hover.png is an image displaed during Onclick of ImageView
<item android:state_focused="true"
android:drawable="@drawable/default_image_hover" />
<item android:state_focused="false"
android:drawable="@drawable/default_image"/>
</selector>
ImageView myImage= (ImageView)findViewById(R.id.image);
myImage.setOnClickListener(this);
您可以使用样式,并根据ImageBUtton的状态设置不同的样式。因为样式支持背景、大小和许多其他属性,可以帮助您根据手头的问题完全控制样式 你如何定义你的ImageButton
??显示XML代码。没有什么特别的,只有一个带有可绘制选择器的ImageButton作为源。除了使用ImageButton
之外,还可以使用ImageView
进行尝试。至于设置图像的问题,您可以使用ImageView
。我希望能够根据状态(按下、选择等)更改图像,这就是为什么我使用ImageButton
。您可以在更改图像的java部分发布吗?您基本上是以编程方式执行选择器的操作,这并不优雅。我真的想要一个基于XML的解决方案。@gdelente我知道它并不优雅,但这与选择器不同。选择器不会调整图像按钮的大小。另外,如果您只寻找XML解决方案,您必须在问题中陈述它。这对您有效吗?ImageView
也没有调整其高度。请尝试使用android:scaleType并将其设置为fitxy。这显然会奏效,但我正在寻找更好的解决方案。
ImageView myImage= (ImageView)findViewById(R.id.image);
myImage.setOnClickListener(this);