【问题标题】:Using one xml for two activities将一个 xml 用于两个活动
【发布时间】:2014-01-03 06:24:36
【问题描述】:

我想在活动中使用地图。

现在我解释整个问题。我有一个活动,我想在其中显示两种类型的地图

  1. 按钮橙色在活动顶部的任何位置的地图
  2. 蓝色按钮在活动顶部的两个点之间绘制折线

我已经完成了这两项任务,但是

  1. Button 1在任意位置的同一个activity bsz地图中可以通过activity扩展类来展示

       public class Map_current_Location extends Activity { 
                // Google Map
                private GoogleMap googleMap;
                @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
        try {
            // Loading map
            initilizeMap();
    
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            // Showing / hiding your current location
            googleMap.setMyLocationEnabled(true);
    
            // Enable / Disable zooming controls
            googleMap.getUiSettings().setZoomControlsEnabled(false);
    
            // Enable / Disable my location button
            googleMap.getUiSettings().setMyLocationButtonEnabled(true);
    
            // Enable / Disable Compass icon
            googleMap.getUiSettings().setCompassEnabled(true);
    
            // Enable / Disable Rotate gesture
            googleMap.getUiSettings().setRotateGesturesEnabled(true);
    
            // Enable / Disable zooming functionality
            googleMap.getUiSettings().setZoomGesturesEnabled(true);
    
            double latitude = 31.510586;
            double longitude = 74.341245;
    
                // Adding a marker
                MarkerOptions marker = new MarkerOptions().position(
                        new LatLng(latitude, longitude))
                        .title("Hello Maps ");
    
    
                // changing marker color
    
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
    
    
                googleMap.addMarker(marker);
    
                // Move the camera to last position with a zoom level
    
                    CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(latitude,
                            longitude)).zoom(30).build();
    
                    googleMap.animateCamera(CameraUpdateFactory
                            .newCameraPosition(cameraPosition));
    
    
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }
    
    /**
     * function to load map If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();
    
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
    
  2. 但不能使用第二个按钮,因为折线和距离只能通过扩展类来完成

公共类 Maps 扩展 FragmentActivity 实现 OnClickListener

请建议我如何在一项活动中同时完成这两项工作 请告诉我是否可以通过sublass完成..

整个活动的 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg" >


    <RelativeLayout
        android:id="@+id/Map_contact_location"
        android:layout_width="fill_parent"
        android:layout_height="250dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/relativeLayout1_contact_location" >

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/Information_contact_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/Map_contact_location" >

        <TextView
            android:id="@+id/textView_details"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="ftggggggggggggggggggggggggggggggg "
            android:textColor="@color/defaultTextColor" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="88dp" >

        <HorizontalScrollView
            android:id="@+id/horizontalScrollView1_Setting"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="13dp"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/call_contact"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:background="@drawable/phone" />

                <Button
                    android:id="@+id/distance_map"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_marginBottom="12dp"
                    android:layout_marginLeft="12dp"
                    android:background="@drawable/directions_icon" />


            </LinearLayout>
        </HorizontalScrollView>
    </RelativeLayout>

</RelativeLayout>

【问题讨论】:

    标签: android google-maps android-fragments


    【解决方案1】:

    您好,请扩展 FragmentActivity,因为它也是扩展 Activity。

    【讨论】:

    • 当我这样做时,我的活动(类)没有开始。并且给我在 xml 中膨胀片段的错误
    • 您是否尝试过 SupportMapFragment 代替 MapFragment?并扩展 FragmentActivity。还要确保您使用了应用的 Map 键并将其与您的包一起放入 AndroidManifest。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多