2012-04-23 25 views
0

ImageButtonのサイズをプログラムで取得して設定するにはどうすればよいですか? Androidは画像サイズよりボタンサイズが大きいようです。 多分私は間違っています。 イメージサイズと同じサイズにしたい。ImageButtonのサイズを取得および設定する方法は?

+0

レイアウトの高さと幅に「wrap_content」を使用するとどうなりますか? –

答えて

3

は、XMLレイアウトで0dpするパディングを設定しよう:

<ImageButton ... 
android:padding="0dp" /> 

画像は、ボタンと同じ大きさでなければなりません。 あなたは@nullするボタン(画像ではなく)透明セットのボタンの背景色を作りたい場合は、次の

<ImageButton ... 
android:background="@null" /> 
+0

ありがとうございます。しかし、それは動作しません。私は別の質問をhttp://stackoverflow.com/questions/10283760/how-to-place-imagebutton-at-x-y-locationに書きました – user1301568

0
int height = button.getLayoutParams().height; 
int width = button.getLayoutParams().width; 

button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT)); 
+0

ありがとう。しかし、それは動作しません。私は別の質問をhttp://stackoverflow.com/questions/10283760/how-to-place-imagebutton-at-x-y-locationに書きました。 – user1301568

0

プログラムで高さと幅を取得するには、次の

ViewGroup.LayoutParams layoutParams = mImageButton.getLayoutParams(); 
int height = layoutParams.height; 
int width = layoutParams.width; 

をプログラム的に設定するには高さと幅:

int myHeight = ContextCompat.getDrawable(getContext(), R.drawable.myImageAsset).getIntrinsicHeight(); 
/*any desired height, but for OP's sake ^^*/ 
int myWidth = ContextCompat.getDrawable(getContext(), R.drawable.myImageAsset).getIntrinsicWidth(); 
/*any desired width, but for OP's sake ^^*/ 
ViewGroup.LayoutParams layoutParams = mImageButton.getLayoutParams(); 
layoutParams.height = myHeight; 
layoutParams.width = myWidth; 
mImageButton.setLayoutParams(layoutParams); 
関連する問題