我正在研究一个项目,并按照这些说明进行操作
(How to add buttons at top of map fragment API v2 layout)
我成功地在Map Fragment(不是支持地图片段)的顶部添加了组件。之后,我对代码进行了更改并执行了以下步骤
(SupportMapFragment Instead of MapFragment)
我将地图片段更改为支持地图片段。除了
,我没有从地图xml中更改任何内容class="com.google.android.gms.maps.MapFragment"
到
class="com.google.android.gms.maps.SupportMapFragment"
但是当我运行我的应用程序时,我的线性布局不再显示在地图上了。
我尝试从FrameLayout
更改为RelativeLayout
并添加了一些属性,但似乎它仍然与我的LinearLayout
重叠。
有什么遗漏或错误的地方吗?
PS。在XML Graphical Layout视图中,我可以看到线性布局在Map上正确定位。
XML图形布局:http://j52.imgup.net/Imagen156161.png
正在运行的应用的屏幕截图: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:我的片段容器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);
}
}
答案 0 :(得分:1)
在您的活动中,请保留对线性布局的引用
LinearLayout root = (LinearLayout) findViewById(R.id.root);
然后在使用片段事务将片段添加到布局后,通过执行
将线性布局置于前面root.bringToFront();
答案 1 :(得分:0)
进入你的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();
但是没有id = to container。