我已经创建了一个名为Graphview
的自定义视图。下面是GraphView类的结构。
public class GraphView extends View {
public GraphView(Context context, float[] values, String title, String[] horlabels, String[] verlabels, boolean type) {
super(context);
........
}
..................
.................
}
我已经使用addview()
将视图添加到了一个表格中。它工作得很好。现在我想设置GraphView
的高度和宽度。如何做到这一点?
发布于 2011-02-18 22:15:18
如果您知道视图的确切大小,只需使用setLayoutParams()
graphView.setLayoutParams(new LayoutParams(width, height));
或者在Kotlin中:
graphView.layoutParams = LayoutParams(width, height)
但是,如果需要更灵活的方法,可以根据可用空间和布局约束(wrap_content
、match_parent
或固定大小)覆盖onMeasure()
以更精确地测量视图。您可以在android docs中找到有关onMeasure()
的更多详细信息。
发布于 2011-02-18 22:09:22
您可以像这样设置高度和宽度:
myGraphView.setLayoutParams(new LayoutParams(width, height));
发布于 2013-06-12 21:22:41
可以在相对布局中设置视图的高度和宽度,如下所示
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = 130;
view.setLayoutParams(params);
https://stackoverflow.com/questions/5042197
复制相似问题