2015-01-16 94 views
0

我已經使用此代碼製作ImageButton &之後,我想在運行時更改它的大小。無論是代碼寫在下面,在android中運行時更改ImageButton的寬度和高度

<ImageButton 
    android:id="@+id/btn_new" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:background="@drawable/untitled3" 
    /> 

public void scaler1() { 
    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth(); 
    int height = display.getHeight(); 
    btn_new.setMinimumWidth(width/4); 
    btn_new.setMinimumHeight(height/6); 
} 

但它不能正常工作,請給它的任何解決方案。

這是實際的代碼我使用

public void scaler1() { 
    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth(); 
    int height = display.getHeight(); 

    edit_screen.setWidth(width); 
    edit_screen.setHeight(height/6); 

    lpsame = (LayoutParams) btn_0.getLayoutParams(); 
    lpsame.width=width/4; 
    lpsame.height=height/6; 

    btn_0.setLayoutParams(lpsame); 



    LayoutParams lpedit = (LayoutParams) edit_screen.getLayoutParams(); 
    lpedit.width=width; 
    lpedit.height=height/6; 

    LayoutParams lpequal = (LayoutParams) btn_0.getLayoutParams(); 
    lpequal.width=width/2; 
    lpequal.height=height/6; 

    edit_screen.setLayoutParams(lpedit); 


    btn_2.setLayoutParams(lpsame); 
    btn_3.setLayoutParams(lpsame); 
    btn_4.setLayoutParams(lpsame); 
    btn_5.setLayoutParams(lpsame); 
    btn_6.setLayoutParams(lpsame); 
    btn_7.setLayoutParams(lpsame); 
    btn_8.setLayoutParams(lpsame); 
    btn_9.setLayoutParams(lpsame); 
    btn_add.setLayoutParams(lpsame); 
    btn_subtrac.setLayoutParams(lpsame); 
    btn_multiply.setLayoutParams(lpsame); 
    btn_divide.setLayoutParams(lpsame); 
    btn_dot.setLayoutParams(lpsame); 
    btn_percentage.setLayoutParams(lpsame); 
    btn_CA.setLayoutParams(lpsame); 
    btn_back.setLayoutParams(lpsame); 
    btn_equal.setLayoutParams(lpequal); 

    editParams = new RelativeLayout.LayoutParams(width, height/6); 
    sameparam= new RelativeLayout.LayoutParams(width/4,height/6); 
    equalparam= new RelativeLayout.LayoutParams(width/2,height/6); 

    edit_screen.setLayoutParams(editParams); 
} 

回答

4

可以更改View的性質使用LayoutParams

LayoutParams lp = (LayoutParams) btn_new.getLayoutParams(); 
lp.width = yourWidth; 
lp.height = yourHeight; 

btn_new.setLayoutParams(lp); 

更新

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(yourWidth, yourHeight); 

yourButtons.setLayoutParams(layoutParams); 
+0

它顯示上setWidth()方法此錯誤,該方法setWidth(INT)是未定義的類型RelativeLayout.LayoutParams –

+0

此錯誤?這是什麼 ? –

+0

它正在爲1 ImageButton,BT當我嘗試它爲多個ImageButton然後,其nt工作 –

0

你有得到你的一個實例視圖,改變其屬性之前

ImageButton btn_new = (ImageButton) findViewById(R.id.btn_new); 
0

嘗試:

Display display = getWindowManager().getDefaultDisplay(); 
     int width = display.getWidth(); 
     int height = display.getHeight(); 

    LayoutParams layoutParams = image_new.getLayoutParams(); 
    layoutParams.width=width/4; 
    layoutParams.height=height/6; 

    image_new.setLayoutParams(layoutParams); 
+0

它正在爲1 ImageButton,BT當我嘗試它爲多個ImageButton然後,它的nt工作 –

+0

你必須得到單獨的按鈕參數,並設置寬度和高度然後分別設置參數。, –