如何以编程方式更改android中图像按钮的大小

时间:2014-07-27 15:26:53

标签: java android resize imagebutton

我必须以编程方式创建几个按钮图像按钮,我不知道如何改变它们的大小。左右改变不起作用。

4 个答案:

答案 0 :(得分:4)

使用此功能。

LinearLayout.LayoutParams params = button.getLayoutParams();
params.width = 80;
button.setLayoutParams(params);

它应该有用

答案 1 :(得分:0)

你可以试试这个:

imageView.getLayoutParams().height = 200;//set appropriate sizes
imageView.getLayoutParams().width= 200;
imageView.requestLayout();//this line redraws the imageview again call only after you set the size

答案 2 :(得分:0)

Here if you want to change the size of the button, you have to change the xml code here it goes.

     <FrameLayout                                        android:layout_width="wrap_content" android:layout_height="wrap_content">           
            <Button     android:id="@+id/saveSearchButton"  android:layout_width="50dp"         android:layout_height="50dp" />
            <ImageView                                      android:layout_width="45dp"         android:layout_height="45dp" android:scaleType="fitXY" android:src="@drawable/ic_menu_save" android:layout_gravity="center"/>
        </FrameLayout>
        <FrameLayout                                        android:layout_width="wrap_content" android:layout_height="wrap_content">           
            <Button     android:id="@+id/clearSearchButton" android:layout_width="50dp"         android:layout_height="50dp" />
            <ImageView                                      android:layout_width="45dp"         android:layout_height="45dp" android:scaleType="fitXY" android:src="@drawable/ic_menu_close_clear_cancel" android:layout_gravity="center"/>

//ImageView Setup
ImageView imageView = new ImageView(this);

//setting height and width of imageview
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

//setting margins around imageimageview
`params.setMargins(10, 10, 10, 10);` //left, top, right, bottom

//adding attributes to the imageview
imageView.setLayoutParams(params);

答案 3 :(得分:0)

imgbtn.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

或:

imgbtn.setLayoutParams(new LinearLayout.LayoutParams(200, 100));
相关问题