谷歌地图活动中的工具栏

时间:2017-10-04 07:56:27

标签: android google-maps android-toolbar

我想添加一个工具栏来映射活动。问题是,当我运行它时,空间就在那里但不显示我的工具栏。

这是我的xml:

<LinearLayout 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"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context="com.ruben.my_app.ui.activity.GoogleMapActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.ActionBar">
    </android.support.v7.widget.Toolbar>

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        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=".ui.activity.GoogleMapActivity" />
</LinearLayout>

这是风格:

<style name="AppTheme.ActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="actionMenuTextColor">#fff</item>
    <item name="android:actionMenuTextColor">#fff</item>
</style>

这是GoogleMapActivity.java:

  public class GoogleMapActivity extends AppCompatActivity implements OnMapReadyCallback {

    @BindView(R.id.toolbar)Toolbar toolbar;

    private GoogleMap mMap;
    public static int color = 0xFF1C1C1C;
    public static int textColor = 0xFFFFFFFF;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google_map);
        setSupportActionBar(toolbar);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        LatLng madrid = new LatLng(40.428462, -3.704952);

        mMap.addMarker(new MarkerOptions().position(madrid).title("Madrid"));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(madrid,17));
    }
}

enter image description here

2 个答案:

答案 0 :(得分:1)

是的,它不会在工具栏中显示任何内容,因为您没有设置要在工具栏内显示的任何视图。

这意味着您可以在工具栏中添加视图,例如

<android.support.v7.widget.Toolbar
    android:id="@+id/top_bar_activity_toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/tool_bar_height"
    android:theme="@style/AppTheme.ActionBar">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/toolbar_left_icon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:gravity="start"
            android:src="@drawable/hamburger"/>

        <TextView
            android:id="@+id/tool_left_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/toolbar_left_icon"
            android:text="Title"/>

    </RelativeLayout>
</android.support.v7.widget.Toolbar>

您可以相应地修改工具栏中的视图。

答案 1 :(得分:0)

这就是我解决它的方式。 (我不需要汉堡包图标)

<LinearLayout 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"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context="com.ilunion.a_muse.ui.activity.GoogleMapActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark">

            <TextView
                android:id="@+id/tool_left_title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerVertical="true"
                android:background="@color/colorPrimaryDark"
                android:textColor="@color/white"
                android:text="Title"/>

    </android.support.v7.widget.Toolbar>

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        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=".ui.activity.GoogleMapActivity" />
</LinearLayout>