前提:
MainActivity.java
Top.java
Mapp.java
activity_main.xml
top.xml
mapp.xml
在activity_main中
<com.example.layout.Top
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/top"
></com.example.layout.Top>
<com.example.layout.Mapp
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></com.example.layout.Mapp>
1、在activity中调用top或者mapp
Mapp view = (Mapp)findViewById(R.id.map);
Top view1 = (Top)findViewById(R.id.top);
2、在top中调用mapp
LayoutInflater factory = LayoutInflater.from(((Activity) getContext()));
View layout = factory.inflate(R.layout.activity_main, null);
Mapp view = (Mapp)layout.findViewById(R.id.map);
PS:这个方法有点问题就是view.invalidate();和view.postInvalidate();无法刷新
如果在map中绘图,在top中刷新map使用这个方法就不行,invalidate执行了但是无法调用onDraw方法,
另一种方法:
在activity中创建方法getMap()
并添加Mapp view = (Mapp)findViewById(R.id.map);view.invalidate();
然后在top中调用这个方法((MainActivity) getContext()).getMap();就可以在top中刷新mapp了