【问题标题】:How do I add a button on fragment layout?(Google maps Android studio)如何在片段布局上添加按钮?(谷歌地图 Android 工作室)
【发布时间】:2017-09-17 05:09:46
【问题描述】:

这是我的maps_activity.xml 文件,当我尝试添加按钮字段时,我收到错误Multiple root tags

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

当我尝试添加时

<Button
android:id="@+id/button"
android:layout_width="146dp"
android:layout_height="wrap_content"
android:text="Open Store" />

我收到一条错误消息Multiple root tags

直到 onCreate 方法之前的我的 MapsActivity 类

public class MapsActivity extends AppCompatActivity
        implements OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        com.google.android.gms.location.LocationListener{

    GoogleMap mGoogleMap;
    SupportMapFragment mapFrag;
    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation;
    Marker mCurrLocationMarker;
    Marker marker;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        getSupportActionBar().setTitle("Map Location Activity");

        mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFrag.getMapAsync(this);

    }

谢谢!

【问题讨论】:

    标签: android google-maps android-fragments


    【解决方案1】:

    每个 XML 布局文件只能有一个“根”标签。通常这将是某种ViewGroup;常见示例包括 LinearLayoutFrameLayoutConstraintLayoutRelativeLayout

    正确的选择取决于您期望的目标是什么。考虑到您只提到了 Google 地图和 Button,要问的问题是您是否希望这两个东西彼此相邻(或彼此上方/下方)或彼此重叠。

    如果你想让它们在彼此之上/之下,选择LinearLayout,然后写如下内容:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    
        <Button
            android:id="@+id/button"
            android:layout_width="146dp"
            android:layout_height="wrap_content"
            android:text="Open Store"/>
    
    </LinearLayout>
    

    如果您希望按钮浮动在地图顶部,请将LinearLayout 更改为FrameLayout(并删除orientation 属性,因为框架布局没有方向)。

    无论哪种方式,您都必须遵守所有布局只能有一个根的规则。这并不意味着所有的布局都只能有一个视图,但是一旦你有多个视图,你就必须想出一个好的盒子来把它们都放进去。

    【讨论】:

      【解决方案2】:

      尝试将相对或线性布局作为父布局,并在父布局内​​添加片段和按钮,如下所示:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">
      
      <fragment xmlns:android="http://schemas.android.com/apk/res/android"
      
          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.commonutils.MapsActivity" />
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:gravity="center">
      
          <Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="GO" />
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-14
        • 1970-01-01
        • 2016-11-19
        • 1970-01-01
        • 2013-01-19
        • 2019-11-02
        • 1970-01-01
        • 2014-02-19
        相关资源
        最近更新 更多