我已使用此代码制作了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);
}
答案 0 :(得分:5)
您可以使用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);
答案 1 :(得分:0)
在更改其属性之前,您必须获取视图的实例
ImageButton btn_new = (ImageButton) findViewById(R.id.btn_new);
答案 2 :(得分: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);