2012-04-23 5 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); 
관련 문제