我正在做一个项目,按照下面的说明
(如何在地图片段API v2布局的顶部添加按钮)
我成功地在地图碎片(不是支持地图碎片)的顶部添加了组件。之后,我对代码进行了更改,按照以下步骤
(SupportMapFragment代替MapFragment)
我改变了我的地图碎片支持地图碎片。除了
之外,我没有更改map xml中的任何内容class="com.google.android.gms.maps.MapFragment"
class="com.google.android.gms.maps.SupportMapFragment"
但是当我运行我的应用程序时,我的线性布局不再显示在地图上。我试着从FrameLayout
到RelativeLayout
,并添加一些属性,但似乎它仍然重叠我的LinearLayout
。
有东西丢失或放错地方了吗?
p。在XML图形布局视图中,我可以看到线性布局在地图上的正确位置。
XML图形布局:http://j52.imgup.net/Imagen156161.png
运行中的app截图:http://i87.imgup.net/Imagen171ef6.png
这是我当前的MapFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<LinearLayout
android:id="@+id/box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_margin="10dp"
android:background="@drawable/button_noborder_background"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingTop="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:padding="2dp"
android:src="@drawable/icon_marker_inactive" />
<TextView
android:id="@+id/txtOrigen"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:gravity="center_vertical"
android:hint="Origen"
android:padding="2dp"
android:textAppearance="@android:style/TextAppearance.Small" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:padding="2dp"
android:src="@drawable/icon_marker_inactive" />
<TextView
android:id="@+id/txtDestino"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white"
android:gravity="center_vertical"
android:hint="Destino"
android:padding="2dp"
android:textAppearance="@android:style/TextAppearance.Small" >
</TextView>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/button_blue_background"
android:gravity="center"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="Ruta"
android:textAppearance="@android:style/TextAppearance.Small"
android:textColor="@color/white" >
</TextView>
</LinearLayout>
</LinearLayout>
</FrameLayout>
activity_main.xml: My fragments container XML
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!--
As the main content view, the view below consumes the entire
space available using match_parent in both dimensions.
-->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--
android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead.
-->
<!--
The drawer is given a fixed width in dp and extends the full height of
the container.
-->
<fragment
android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
从MainActivity.java, onNavigationDrawerItemSelected方法,我创建MapFragment
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment;
FragmentManager fragmentManager;
switch (position) {
default:
case 0:
fragment = MapFragment.newInstance(position + 1);
onSectionAttached(position + 1);
break;
}
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment)
.commit();
}
MapFragment.java
import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
/**
* A placeholder fragment containing a simple view.
*/
public class MapFragment extends SupportMapFragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
private static View rootView;
LocationManager locationManager;
static GoogleMap map;
/**
* Returns a new instance of this fragment for the given section number.
*/
public static SupportMapFragment newInstance(int sectionNumber) {
SupportMapFragment fragment = new SupportMapFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public MapFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.fragment_mapa, container,
false);
}catch(Exception e){
}
// Location Service
locationManager = (LocationManager) getActivity().getSystemService(
Context.LOCATION_SERVICE);
// Inicializacion del mapa
map = getMapFragment().getMap();
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(getArguments().getInt(
ARG_SECTION_NUMBER));
}
private SupportMapFragment getMapFragment() {
FragmentManager fm = null;
// Log.d(TAG, "sdk: " + Build.VERSION.SDK_INT);
// Log.d(TAG, "release: " + Build.VERSION.RELEASE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// Log.d(TAG, "using getFragmentManager");
fm = getFragmentManager();
} else {
// Log.d(TAG, "using getChildFragmentManager");
fm = getChildFragmentManager();
}
return (SupportMapFragment) fm.findFragmentById(R.id.map);
}
}
在你的活动中,保持对你的线性布局的引用
LinearLayout root = (LinearLayout) findViewById(R.id.root);
然后在片段被添加到使用片段事务的布局后,通过执行
将你的线性布局带到前面root.bringToFront();
进入xml文件
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
应该<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
因为您初始化到导航器抽屉:
fragmentManager.beginTransaction().replace(R.id.container, fragment)
.commit();