Android set height and width of Custom view programmatically

微笑、不失礼 提交于 2019-11-27 06:15:06

The easy way is to set the size programatically like that :

graphView.setLayoutParams(new LayoutParams(width, height));

This is fine if you know the exact size of the view. However, if you want a more flexible approach, you can override the onMeasure() method to measure the view more precisely depending on the space available and layout constraints (wrap_content, match_parent, or a fixed size).

You can find an example on how to override onMeasure() by looking at the android docs and the LabelView sample in your SDK directory.

You can set height and width like this:

myGraphView.setLayoutParams(new LayoutParams(width, height));
Rakshi

you can set the height and width of a view in a relative layout like this

ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = 130;
view.setLayoutParams(params);
Ujjwal
spin12.setLayoutParams(new LinearLayout.LayoutParams(200, 120));

spin12 is your spinner and 200,120 is width and height for your spinner.

This is a Kotlin based version, assuming that the parent view is an instance of LinearLayout.

someView.layoutParams = LinearLayout.LayoutParams(100, 200)

This allows to set the width and height (100 and 200) in a single line.

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