不同状态图像大小的ImageButton

10

我如何防止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>

除了使用ImageButton,还可以尝试使用ImageView。只要涉及设置图像,就可以使用ImageView - GrIsHu
正如您在XML文件中所看到的,ImageButton的源是一个选择器,在更改图像时没有涉及Java代码。 - gdelente
adjustViewBounds 关掉。这会使你的 ImageButton 边界缩小到与当前图像一样大。 - Jason Robinson
这正是我想要的,但它不起作用。 - gdelente
选择器的大小应该是最大图像的尺寸。 - njzk2
显示剩余6条评论
6个回答

6

不要使用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>

image_selection.xml:

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

SampleActivity.java:

  ImageView myImage= (ImageView)findViewById(R.id.image);
    myImage.setOnClickListener(this);

0
不要使用 ImageButton,只需使用 Image 并为其提供 onClick 方法即可。

这显然可以解决问题,但我正在寻找更好的解决方案。 - gdelente

0

您可以使用样式并根据ImageButton的状态设置不同的样式。因为样式支持背景、大小和许多其他属性,这有助于您根据手头的问题完全控制样式。


0

就像你可能已经知道的那样,在选择器中设置可绘制项并没有简单的方法, 而且我认为没有基于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>

然后您需要将此代码添加到onCreate或onResume中

    final ImageButton button = (ImageButton) findViewById(R.id.button);

    button.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN)
                button.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("pressed_image", "drawable", getPackageName())));
            else if(event.getAction() == MotionEvent.ACTION_UP)
                button.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("default_image", "drawable", getPackageName())));

            return false;
        }
    });

1
你基本上是在以编程的方式完成选择器所做的事情,这并不优雅。我真的希望有一个基于XML的解决方案。 - gdelente
@gdelente 我知道这不够优雅,但这与选择器不同。选择器不会调整你的ImageButton大小。而且,如果你只寻找XML解决方案,你必须在问题中说明。 - akaya

0
正如其他人所提到的,最好使用ImageView,因为它会自动调整高度。将属性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>

或者您可以将LinearLayout设置为可点击,并添加一个背景资源(未被点击时透明,被点击时变色)作为选择器。


这对你行吗?ImageView也没有调整其高度。 - gdelente
1
尝试使用android:scaleType,并将其设置为fitXY。 - Nickolaus

0

尝试一下

android:background="@drawable/tab_background_selector" 

而不是

android:src="@drawable/tab_background_selector"

希望这有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接