Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 何时获取自定义视图';s的宽度和高度属性_Android_Android Fragments_Android View - Fatal编程技术网

Android 何时获取自定义视图';s的宽度和高度属性

Android 何时获取自定义视图';s的宽度和高度属性,android,android-fragments,android-view,Android,Android Fragments,Android View,我有一个名为LineGraphView的类,它是视图的子类。我希望能够向该视图传递一些数据,以便它可以在其onDraw()方法中相应地绘制 分析片段(其中引用视图并从中调用方法) 我的问题是,如果我希望能够使用getWidth()和getHeight()访问该视图的宽度和高度属性,那么我什么时候能够这样做,因为到目前为止返回的都是0 我是否需要创建一个监听器,让LineGraphView的父级知道它已准备好被访问 提前感谢。答案如下: 基本上,调用getWidth/height太早了(在onCr

我有一个名为
LineGraphView
类,它是
视图
的子类。我希望能够向该视图传递一些数据,以便它可以在其
onDraw()方法中相应地绘制

分析片段(其中引用视图并从中调用方法)

我的问题是,如果我希望能够使用
getWidth()
getHeight()
访问该视图的宽度和高度属性,那么我什么时候能够这样做,因为到目前为止返回的都是
0

我是否需要创建一个监听器,让
LineGraphView的
父级知道它已准备好被访问

提前感谢。

答案如下:
基本上,调用getWidth/height太早了(在onCreate期间)。我相信您可以使用处理程序在onCreate之后获取它。您也可以这样做:(使用onWindowFocusChanged)。

如果您已经在创建自己的自定义视图,我只需添加一些带有getter/setter的私有变量,以获得需要传递的信息。从这里开始,只需在onDraw()方法中处理图形——从这里,您可以获得视图的宽度和高度(或画布的宽度和高度,可能是相同的值)

问题是我需要从我的片段发送数据(我的视图就是从这个片段膨胀出来的)。我什么时候可以这样做?如果你有数据的公共设置器,你可以随时发送它。我可能误解了这个问题,但在发送时应该没关系,只要确保在setter中使视图无效和/或请求布局,以确保它刷新即可。或者,如果您希望采用侦听器路径,则可以使用OnPreDrawListener。我按照你说的第一种方式成功了。非常感谢你的帮助
LineGraphView altitudeGraph;
LineGraphView speedGraph;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
    super.onCreateView(inflater, group, saved);

    View view = inflater.inflate(R.layout.analysisfrag, group, false);
    this.altitudeGraph = (LineGraphView) view.findViewById(R.id.linegraph_Altitude);
    this.speedGraph = (LineGraphView) view.findViewById(R.id.linegraph_Speed);

    return view;
}