【问题标题】:Add button on top of map fragmet在地图片段顶部添加按钮
【发布时间】:2018-03-12 14:33:37
【问题描述】:

我正在尝试在我的 Google 地图片段顶部实现一个按钮,但我看不到它。

这是代码(我也尝试将按钮放在片段之外):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.car.MapsActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|top"
        android:text="Demo Button"
        android:padding="10dp"
        android:layout_marginTop="20dp"
        android:paddingRight="10dp"/>

</fragment>

另外,这部分用到的java文件如下:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

编辑:这是启动地图的代码,可能会导致问题:

    //this function initializes the map presentation
private void initMap() {
    SupportMapFragment map_fragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.main_map);
    map_fragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            user_map = googleMap;
            Toast.makeText(getApplicationContext(), "Map is ready!", Toast.LENGTH_SHORT).show();

            if (mLocationPermissionGranted) {
                getDeviceLocation();

                if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext()
                        , Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                user_map.setMyLocationEnabled(true);

                user_map.getUiSettings().setMyLocationButtonEnabled(true);

            }
        }
    });
}

这是我得到的输出(仅显示地图,没有按钮): https://prnt.sc/iq5ic3

【问题讨论】:

  • 尝试在片段标签之前写按钮标签。
  • 也试过了,没有成功。
  • 还是不行……

标签: android xml google-maps


【解决方案1】:

试试这个。删除了该按钮的一些属性 b'z 被隐藏。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <fragment
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.user.car.MapsActivity"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:padding="10dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:paddingRight="10dp"
        android:text="Demo Button" />

</RelativeLayout>

【讨论】:

  • 我可以在预览中看到按钮:prntscr.com/iq5et8。但不是在我的手机上
  • 尝试清理构建并再次运行。可能是缓存问题。
  • 粘贴有问题的屏幕截图或其他链接。它被我屏蔽了。
  • 相同的结果...不知何故地图会覆盖除顶部导航栏以外的所有内容...
【解决方案2】:

试试这个

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:theme="@style/ThemeOverlay.AppCompat.Dark">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.car.MapsActivity"></fragment>

</FrameLayout>

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="20dp"
    android:text="Demo Button" />

输出:

【讨论】:

  • 知道该怎么做吗?
  • @Mr.Code 如果我的回答有效,请不要忘记接受
  • 我不知道为什么它仍然不起作用....即使我删除了整个片段,它仍然显示地图。它与 Java 代码有关
  • 看看我在帖子中添加的编辑,可能是它的问题
  • @Mr.Code 只要检查一下你是否正确地膨胀了 xml...setContentView(R.layout.activity_maps); 我检查了你的 java 代码,它是完美的
【解决方案3】:

试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.example.user.car.MapsActivity"></fragment>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/map"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="20dp"
    android:padding="10dp"
    android:paddingRight="10dp"
    android:text="Demo Button" />

将按钮放在RelativeLayout

【讨论】:

  • 感谢您的发帖。不幸的是,我仍然看不到按钮。
  • 更改片段的布局高度以包裹内容并重试
  • 这有点奇怪,即使我把高度设置为 10dp 如果覆盖整个屏幕......
  • 尝试清理您的项目并在我的 android studio 预览中正确重建它的显示
  • 我在预览中也看到了它,但在我的手机中它没有显示
猜你喜欢
  • 2013-01-19
  • 1970-01-01
  • 2013-05-25
  • 1970-01-01
  • 2018-05-15
  • 1970-01-01
  • 2018-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多