2012-06-12 51 views
1

這是我的代碼集ImageButton的高度和寬度代碼

ImageButton btnPlayVideo = (ImageButton) findViewById(R.id.btnPlayVideo); 
btnPlayVideo.setBackgroundResource(R.drawable.play_icon_grey); 
btnPlayVideo.setAdjustViewBounds(true); 
btnPlayVideo.setMaxHeight(10); 
btnPlayVideo.setMaxWidth(10); 

,並在我的XML

<ImageButton 
android:id="@+id/btnPlayVideo" 
android:layout_width="90dp" 
android:layout_height="90dp" 
android:background="@android:color/transparent" 
android:layout_span="2" /> 

但似乎setmaxheight和layout_height和寬度甚至不工作。那麼我如何設置圖像按鈕的尺寸?

回答

4

您將需要使用android:scaleType來調整它的大小。

<ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/ic_launcher" 
     android:scaleType="fitXY" /> 

它產生以下輸出。

enter image description here

3

您可以通過

android.view.ViewGroup.LayoutParams params = btnPlayVideo.getLayoutParams(); 
params.height = myHeight; 
params.width = myWidth; 
btnPlayVideo.setLayoutParams(params); 

從代碼做到這一點,但可能你的問題奠定不在代碼,但在佈局 - 我看你使用android:layout_span="2" - 這是否意味着btnPlayVideo是在TableLayout?如果是這樣,你必須調整這個TableLayout的尺寸,因爲它的孩子將始終有寬度和高度fill_parent

0

我用的指標:

DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics); 

btnPlayVideo.setMinimumHeight(metrics.heightPixels/(amount)); 
btnPlayVideo.setMinimumWidth(metrics.widthPixels/ (amount));