Android Google Map API V2 - 片段上方的按钮

时间:2014-04-03 15:49:10

标签: android google-maps button android-fragments

我设法在屏幕上显示地图。我正在尝试在地图上方创建按钮,然后为此添加操作。例如。该按钮将更改地图的位置。我基本上试图调整我的代码:https://www.youtube.com/watch?v=awX5T-EwLPc。但是,我正在使用"扩展Fragment Activity"这让一切都搞砸了。

我的问题:我无法在地图上方创建按钮,它放在地图内。我正在尝试在地图上方创建一个按钮,并分配代码,以便我可以移动到地图上的某个位置等。这是我的代码:

MainActivity.java

package com.example.theapp;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener
{

    Button button;
    TextView text;

   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

        button=(Button)findViewById(R.id.button1);
        text=(TextView)findViewById(R.id.textView1);
   }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        text.setText("The button worked!");

    }
}

activity_main.xml中

<?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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/map"
        android:layout_alignTop="@+id/map"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignRight="@+id/map"
        android:layout_marginRight="39dp"
        android:text="TextView" />

</RelativeLayout>

从代码中,我的应用程序在地图上显示一个按钮和一个文本视图。单击按钮时,它意味着更改文本,但它不会执行任何操作。我已经研究了教程,从它的外观来看,我将不得不创建多个片段?

问题:如何在该页面上创建一个按钮,然后将用户指向地图?

提前谢谢。

2 个答案:

答案 0 :(得分:0)

您没有为按钮设置任何功能......您应该使用

button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                text.setText("that's how you do it!");
            }
        });

答案 1 :(得分:0)

你不必担心&#34; FragmentActivity&#34;直到和除非你使用片段类。   只需将此作为正常活动使用。它会运作良好。

    package com.example.theapp;

     import android.os.Bundle;
     import android.support.v4.app.FragmentActivity;
     import android.view.View;
     import android.view.View.OnClickListener;
     import android.widget.Button;
     import android.widget.TextView;

       public class MainActivity extends FragmentActivity implements OnClickListener
     {

       Button button;
       TextView text;

        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(this);
         text=(TextView)findViewById(R.id.textView1);
     }

        @Override
       public void onClick(View arg0) {
       // TODO Auto-generated method stub
        text.setText("The button worked!");

      }
       }