ImageButton具有不同的状态图像大小

时间:2013-02-18 03:47:48

标签: android android-layout imagebutton

如何防止ImageButton具有固定大小? 选择器中使用的drawable具有不同的大小,我希望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>

6 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

您可能已经知道在选择器中设置drawable没有简单的方法, 我认为你的问题没有基于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;
        }
    });

答案 2 :(得分:0)

不要使用ImageButton,只需使用Image,并使用onClick方法提供它

答案 3 :(得分: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可点击并添加背景资源(未点击时为透明,点击时为彩色)以充当选择器。

答案 4 :(得分:0)

尝试

android:background="@drawable/tab_background_selector" 

而不是

android:src="@drawable/tab_background_selector"
希望有所帮助。

答案 5 :(得分:0)

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

相关问题