Как добавить кнопку и наложить ее на мой фрагмент карты Google - PullRequest
0 голосов
/ 10 января 2019

Итак, у меня есть GoogleMap, использующий мой ключ API. Я могу найти свое местоположение, но при открытии приложение не имеет кнопок. Если я попытаюсь нажать и перетащить какие-либо кнопки через макет дизайна Android, это не позволит.

Вот как выглядит мой activity_maps.xml:

<?xml version="1.0" encoding="utf-8"?>
<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=".MapsActivity" />

Когда вы смотрите на вид телефона Android рядом с ним, он просто говорит, и я не уверен, как добавить кнопку поверх этой карты или даже под ней, если это возможно. Я знаю о ConstraintLayout и FrameLayout, но при добавлении дополнительного кода я просто получаю «ошибку нескольких корней» и не могу достичь того, что ищу.

Кто-нибудь может мне помочь?

Ответы [ 3 ]

0 голосов
/ 10 января 2019

Вы должны обернуть свой фрагмент с помощью FrameLayout, LinearLayout, RelativeLayout или ConstraintLayout и затем установить дополнительные кнопки.

Вместо этого:

<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=".MapsActivity" />

Это должно выглядеть так:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

<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=".MapsActivity"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_margin="15dp"
    tools:text="Button one"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_margin="15dp"
    tools:text="Button two"/>

 </RelativeLayout>

Это просто пример с RelativeLayout.

0 голосов
/ 10 января 2019

Вот пример с макетом ограничения

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MapsActivity">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_margin="16dp"

        />

</android.support.constraint.ConstraintLayout>
0 голосов
/ 10 января 2019

Возможно, более простое решение - установить оверлей перед вашей картой, используя FrameLayout или RelativeLayout, и обрабатывать их как обычные кнопки в вашей деятельности. Вы должны объявить свои слои в обратном порядке, например, отобразить перед кнопками, попробуйте следующий макет и посмотрите, работает ли он для вас

 <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"
        tools:context=".MapActivity" >

        <fragment xmlns:map="http://schemas.android.com/apk/res-auto"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:scrollbars="vertical"  
            class="com.google.android.gms.maps.SupportMapFragment"/>

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:orientation="horizontal" 
            android:background="#80000000"
            android:padding="4dp" >
    </FrameLayout>
...