26

imageButton画像にぴったり合うようにサイズを変更する方法を誰か教えてもらえますか? これは私が試したコードですが、 を使用して見つけている位置に画像が配置されていますがandroid:scaleType、 のサイズを縮小することはできませんimageButton。この問題を修正するのを手伝ってください。私が試したコードは次のとおりです。

<ImageButton>
android:id="@+id/Button01"
android:scaleType="fitXY" // i have tried all the values for this attr
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="false"
android:paddingLeft="10dp"
android:src="@drawable/eye"> // this is the image(eye)
</ImageButton>
4

8 に答える 8

32
android:background="@drawable/eye"

自動的に動作します。

android:src="@drawable/eye"

ボタンの幅と高さの画像のサイズを変更するすべての問題で使用したものでした...

于 2011-03-21T03:42:39.723 に答える
15

プロパティ「src」で画像を設定しています

android:src="@drawable/eye">

" background" プロパティの代わりに " src" プロパティを使用してください:

android:background="@drawable/eye"

お気に入り:

<ImageButton
  android:id="@+id/Button01"
  android:scaleType="fitXY" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:cropToPadding="false"
  android:paddingLeft="10dp"
  android:background="@drawable/eye"> // this is the image(eye)
</ImageButton>
于 2010-07-23T17:21:14.117 に答える
3

おそらく、プログラムでボタンのサイズを変更する必要があります。onCreate()メソッドで画像を明示的にロードし、そこでボタンのサイズを変更する必要があります。

public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main);  
  ImageButton myButton = (ImageButton) findViewById(R.id.button);  
  Bitmap image = BitmapFactory.decodeResource(R.drawable.eye);  
  myButton.setBitmap(image);  
  myButton.setMinimumWidth(image.getWidth());  
  myButton.setMinimumHeight(image.getHeight());
  ...
}

setMinimumXの仕様によると(幅と高さは親ビューに依存しているため)、動作が保証されていませんが、ほとんどすべての状況で正常に動作するはずです。

于 2010-07-23T16:32:48.870 に答える
2

次のようにlayout_widthとlayout_heightを指定しようとしましたか?wrap_contentで設定しているため、画像ボタンはソース画像の高さと幅のサイズに拡大されます。

<blink>    
  <ImageButton>
    android:id="@+id/Button01"
    android:scaleType="fitXY" 
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:cropToPadding="false"
    android:paddingLeft="10dp"
    android:src="@drawable/eye"> 
  </ImageButton>
</blink>
于 2011-06-30T17:08:58.543 に答える
2

src 属性を使用して使用する必要はありません

間違った方法 (画像がボタンに収まらない)

android:src="@drawable/myimage"

正しい方法は、背景属性を使用することです

<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/skin" />

スキンはxmlです

skin.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- <item android:drawable="@drawable/button_disabled" android:state_enabled="false"/> -->
    <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
    <!-- <item android:drawable="@drawable/button_focused" android:state_focused="true"/> -->
    <item android:drawable="@drawable/button_normal"/>

</selector>

button_pressed.png と button_normal.png の使用

これは、pressed、normal、disabled、focused の 4 つの状態を持つスキン ボタンの作成にも役立ちます。すべてのpngのサイズを同じに保つようにしてください

于 2013-10-23T10:10:47.053 に答える
2

ScaleType centerInside を使用してみてください。

ScaleTypes は Eclipse レイアウト デザイナーで適切にレンダリングされないため、実行中のアプリでテストしてください。

于 2011-06-01T15:23:17.283 に答える
1

背景を透明に設定することもできます。ボタンはあなたのアイコンに合うように見えます。

 <ImageButton
    android:id="@+id/Button01"
    android:scaleType="fitcenter" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:cropToPadding="false"
    android:paddingLeft="10dp"
    android:background="@android:color/transparent"
    android:src="@drawable/eye" />
于 2012-05-23T01:16:22.890 に答える
0

あなたはすでにこの問題を解決していると思います。他の回答が示唆するように

android:background="@drawable/eye"

利用可能です。でも私は〜がいい

android:src="@drawable/eye"
android:background="00000000" // transparent

それもうまく機能します(もちろん、以前のコードは画像を背景として設定し、他のコードは画像を画像として設定します)しかし、選択した回答によると、9パッチを意味していたと思います。

于 2012-10-17T06:17:15.270 に答える