如何在MapsActivity中创建浮动操作按钮。地图活动有全屏主题?

时间:2016-01-15 15:11:44

标签: android google-maps

我正在处理地图活动,并且在尝试放置浮动操作按钮时我遇到了错误。

  • 我想在地图活动中使用全屏功能的浮动操作按钮。

这是我的XML文件。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_marginTop="0dp"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    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.example.avinashk.rns.MapsActivity"
    tools:layout="@layout/abc_action_bar_title_item" />
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />
</LinearLayout>

这是地图活动的清单。

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />
    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>

这是地图活动的onCreate。

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(MapsActivity.this);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //double result = CalculationByDistance(rns,);
            Snackbar.make(view, "You are DD distance from the college", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });}

渲染问题日志是:

The following classes could not be instantiated:
- android.support.design.widget.FloatingActionButton (Open Class, Show Exception, Clear Cache)
 Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE  Exception Details java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.   at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:34)   at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:110)   at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:104)   at java.lang.reflect.Constructor.newInstance(Constructor.java:422)   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)   at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:835)   at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)   at android.view.LayoutInflater.rInflate(LayoutInflater.java:811)   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)   at android.view.LayoutInflater.inflate(LayoutInflater.java:515)   at android.view.LayoutInflater.inflate(LayoutInflater.java:394)

1 个答案:

答案 0 :(得分:2)

确保您已正确添加Support Library,如下所示:

public class MapsActivity extends AppCompatActivity

你没有检查这个错误:

  

提示:在自定义视图中使用View.isInEditMode()来跳过代码或显示   IDE异常详细信息中显示的示例数据   java.lang.IllegalArgumentException: 您需要使用Theme.AppCompat   设计库的主题(或后代)

在您的依赖项中检查:

compile 'com.android.support:support-v4:23.1.1'

同步它,你准备好了。

还有一件事,如果您尝试按照以下链接进行操作,我宁愿使用来自支持库的CoordinatorLayout

https://www.google.com/design/spec/components/buttons-floating-action-button.html#buttons-floating-action-button-transitions

可以是这样的:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fab_coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EEEEEE"
    android:fitsSystemWindows="true">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

       <!--your fragment could be here-->

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/ic_remove_acc"
        app:backgroundTint="#DD2C00"
        app:fabSize="normal"
        app:layout_anchor="@id/fab_coordinator_layout"
        app:layout_anchorGravity="right|bottom" />

</android.support.design.widget.CoordinatorLayout>