How to change size of imagebutton in android programatically

一世执手 提交于 2019-12-22 10:28:32

问题


I have to create few buttons imagebuttons programatically and i don't know how to change size of them. Changing left and right does not work.


回答1:


Use this.

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

it should work




回答2:


You can try this:

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



回答3:


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

or :

imgbtn.setLayoutParams(new LinearLayout.LayoutParams(200, 100));



回答4:


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);


来源:https://stackoverflow.com/questions/24982432/how-to-change-size-of-imagebutton-in-android-programatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!