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

6 に答える 6

6

を使用する代わりに、 をImageButton使用ImageViewImageButtonますImageSizeImageViewあなたの問題に対して私が提案できる最良の代替方法です。次の方法でそれを達成します。

レイアウト.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);
于 2013-02-27T06:30:42.163 に答える
0

スタイルを使用し、ImageBUtton の状態に基づいてさまざまなスタイルを設定できます。スタイルは背景、サイズ、その他多くのプロパティをサポートしているため、目前の問題に応じてスタイリングを完全に制御できます。

于 2013-02-27T20:26:37.500 に答える
0

他の人が述べたようにImageView、高さを調整するので、 を使用するのが最善です。setClickableボタンのように使用するようにプロパティを設定します。RelativeLayoutまた、あなたを aに交換します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クリック可能にして背景リソース (クリックしないと透明、クリックすると色付き) を追加して、セレクターとして機能させることもできます。

于 2013-02-21T21:56:11.773 に答える
0

ImageButton を使用しないでください。単に Image を使用し、onClick メソッドで提供してください。

于 2013-02-21T21:44:28.310 に答える
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;
        }
    });
于 2013-02-21T09:45:13.930 に答える
0

試す

android:background="@drawable/tab_background_selector" 

の代わりに

android:src="@drawable/tab_background_selector"

それが役立つことを願っています。

于 2013-02-27T06:16:34.730 に答える