如何将地图活动添加到底部导航?

Posted

技术标签:

【中文标题】如何将地图活动添加到底部导航?【英文标题】:How to add maps activity to bottom navigation? 【发布时间】:2020-02-08 13:13:24 【问题描述】:

我想把我的谷歌地图,但我找不到教程,放到底部导航栏中,有人可以帮我吗?

【问题讨论】:

我在下面给出了 tablayout 的代码,我觉得这可以帮助你 【参考方案1】:

在我的情况下,我已将此代码用于 tablayout,这对我有用,您可以检查这是否可以帮助您

MainActivity 类:

public class MainActivity extends AppCompatActivity 

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Get the ViewPager and set it's PagerAdapter so that it can display items
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        PagerAdapter pagerAdapter =
                new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
        viewPager.setAdapter(pagerAdapter);

        // Give the TabLayout the ViewPager
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(viewPager);

        // Iterate over all tabs and set the custom view
        for (int i = 0; i < tabLayout.getTabCount(); i++) 
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tab.setCustomView(pagerAdapter.getTabView(i));
        
    


    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        int id = item.getItemId();

        if (id == R.id.action_settings) 
            return true;
        

        return super.onOptionsItemSelected(item);
    

    class PagerAdapter extends FragmentPagerAdapter 

        String tabTitles[] = new String[]  "Tab One", "Tab Two", "Tab Three", ;
        Context context;

        public PagerAdapter(FragmentManager fm, Context context) 
            super(fm);
            this.context = context;
        

        @Override
        public int getCount() 
            return tabTitles.length;
        

        @Override
        public Fragment getItem(int position) 

            switch (position) 
                case 0:
                    return new BlankFragment();
                case 1:
                    return new BlankFragment();
                case 2:
                    return new MapFragment();
            

            return null;
        

        @Override
        public CharSequence getPageTitle(int position) 
            // Generate title based on item position
            return tabTitles[position];
        

        public View getTabView(int position) 
            View tab = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_tab, null);
            TextView tv = (TextView) tab.findViewById(R.id.custom_text);
            tv.setText(tabTitles[position]);
            return tab;
        

    

MapFragment,请注意它直接扩展了 SupportMapFragment,而不是在其中包含嵌套的 SupportMapFragment:

public class MapFragment extends SupportMapFragment implements
        OnMapReadyCallback 

    private final LatLng HAMBURG = new LatLng(53.558, 9.927);
    private final LatLng KIEL = new LatLng(53.551, 9.993);

    private static final String ARG_SECTION_NUMBER = "section_number";

    private GoogleMap mMap;
    private Marker marker;

    public MapFragment() 
    

    @Override
    public void onResume() 
        super.onResume();

        Log.d("MyMap", "onResume");
        setUpMapIfNeeded();
    

    private void setUpMapIfNeeded() 

        if (mMap == null) 

            Log.d("MyMap", "setUpMapIfNeeded");

            getMapAsync(this);
        
    

    @Override
    public void onMapReady(GoogleMap googleMap) 
        Log.d("MyMap", "onMapReady");
        mMap = googleMap;
        setUpMap();
    

    private void setUpMap() 

        mMap.setMyLocationEnabled(true);
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        mMap.getUiSettings().setMapToolbarEnabled(false);


        mMap.setMyLocationEnabled(true);
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        mMap.getUiSettings().setMapToolbarEnabled(false);

        Marker hamburg = mMap.addMarker(new MarkerOptions().position(HAMBURG)
                .title("Hamburg"));
        Marker kiel = mMap.addMarker(new MarkerOptions()
                .position(KIEL)
                .title("Kiel")
                .snippet("Kiel is cool")
                .icon(BitmapDescriptorFactory
                        .fromResource(R.mipmap.ic_launcher)));

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

        mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    

Activity 的布局 xml:

<RelativeLayout
    android:id="@+id/main_layout"
    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_
    android:layout_
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_
        android:layout_
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        app:tabMode="fixed"
        android:layout_
        android:layout_
        android:layout_below="@+id/toolbar"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        app:tabTextColor="#d3d3d3"
        app:tabSelectedTextColor="#ffffff"
        app:tabIndicatorColor="#ff00ff"
        android:minHeight="?attr/actionBarSize"
        />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_
        android:layout_
        android:layout_below="@id/tab_layout"/>

</RelativeLayout>

custom_tab.xml,即每个tab的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_
    android:layout_>
    <TextView
        android:id="@+id/custom_text"
        android:layout_
        android:layout_
        android:background="?attr/selectableItemBackground"
        android:gravity="center"
        android:textSize="16dip"
        android:textColor="#ffffff"
        android:singleLine="true"
        />
</LinearLayout>

【讨论】:

以上是关于如何将地图活动添加到底部导航?的主要内容,如果未能解决你的问题,请参考以下文章

使用喷气背包导航将自定义过渡动画添加到底部导航设置

如何将片段目标绑定到底部导航栏中的菜单项?

如何将菜单动态添加到底部导航视图?

想要在一项活动中添加底部和侧边导航,但如何?

css如何将导航绝对定位到页面底部

如何使用同一个应用制作抽屉式导航和底部导航?