猿问

Android studio Java - 地图上的底部工作表视图

对于android编程,java新手,我正在尝试创建一个“简单..”BottomSheet,当我单击我的位置之一时,它必须出现。实际上我已经能够创建我的地图并加载我的点数据。我的地图在一个片段中..


    <?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent">


<fragment

    android:id="@+id/map"

    android:name="com.google.android.gms.maps.SupportMapFragment"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:clickable="true"


    />


</RelativeLayout>

然后我查看在线教程创建了我的 BottomSheet。


<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinaCoordinatorLayout        xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.chargesplit.android.activities.MainActivity">



 <android.support.v4.widget.NestedScrollView

    android:id="@+id/nestedScrollView"

    android:layout_width="match_parent"

    android:layout_height="300dp"

    android:background="@android:color/holo_green_light"

    android:clipToPadding="true"

    app:behavior_hideable="true"

    app:behavior_peekHeight="0dp"

    app:layout_behavior="@string/bottom_sheet_behavior">


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical"

        android:padding="16dp">

    </LinearLayout>


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


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


慕田峪4524236
浏览 91回答 1
1回答

炎炎设计

发生此错误是因为您的活动无法找到底部工作表视图,为什么呢?因为您的底部工作表视图未包含在activity_main.xml布局中。换句话说,您MainActivity.java只能查看activity_main.xml,但它仍然不知道底部工作表的其他 xml 文件。activity_main要解决此问题:将布局中的代码替换为<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinaCoordinatorLayout&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; tools:context="com.chargesplit.android.activities.MainActivity">&nbsp; &nbsp; <fragment&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/map"&nbsp; &nbsp; &nbsp; &nbsp; android:name="com.google.android.gms.maps.SupportMapFragment"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="fill_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="fill_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:clickable="true"/>&nbsp; &nbsp; &nbsp;<android.support.v4.widget.NestedScrollView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/nestedScrollView"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="300dp"&nbsp; &nbsp; &nbsp; &nbsp; android:background="@android:color/holo_green_light"&nbsp; &nbsp; &nbsp; &nbsp; android:clipToPadding="true"&nbsp; &nbsp; &nbsp; &nbsp; app:behavior_hideable="true"&nbsp; &nbsp; &nbsp; &nbsp; app:behavior_peekHeight="0dp"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_behavior="@string/bottom_sheet_behavior">&nbsp; &nbsp; &nbsp; &nbsp; <LinearLayout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:orientation="vertical"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:padding="16dp">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="ciao"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@android:color/white"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textSize="24sp" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="16dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="ciao2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:textColor="@android:color/white" />&nbsp; &nbsp; &nbsp; &nbsp; </LinearLayout>&nbsp; &nbsp; </android.support.v4.widget.NestedScrollView></android.support.design.widget.CoordinaCoordinatorLayout>注意:为了使您的布局看起来整洁,您可以将底部工作表视图分离到一个单独的布局中,这样您就可以将其放入<android.support.v4.widget.NestedScrollView>bottom_sheet.xml中,然后从activity_main.xml中引用它<include layout="@layout/bottom_sheet" />希望对您有所帮助。如果您需要进一步的帮助,请告诉我
随时随地看视频慕课网APP

相关分类

Java
我要回答