【问题标题】:BottomNavigationView not showing in my activityBottomNavigationView 未显示在我的活动中
【发布时间】:2016-10-20 12:02:50
【问题描述】:

我正在使用 link 实现 BottomNavigationView 我一步一步实现了一切,但我的导航视图没有显示在屏幕底部。

这就是我所做的。

public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

    Intent intent = null;
    BottomNavigationView navigationView;

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

        navigationView = (BottomNavigationView) findViewById(R.id.navigation); 

        navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                int id = item.getItemId();
                if(id == R.id.program){
                    intent = new Intent(MainActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                if(id == R.id.access){
                    try {
                        manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                            Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivity(myIntent);
                            overridePendingTransition(R.anim.push_up_in,
                                    R.anim.push_up_out);
                        } else {
                            intent = new Intent(MainActivity.this, Access.class);
                            startActivity(intent);
                            finish();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return true;
                }
                if(id == R.id.informations){
                    intent = new Intent(MainActivity.this, Information.class);
                    startActivity(intent);
                    finish();
                    return true;
                }
                if(id == R.id.contact){
                    intent = new Intent(MainActivity.this, Contact.class);
                    startActivity(intent);
                    finish();
                    return true;
                }
                return false;
            }
        });
     }
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu = navigationView.getMenu(); <---- // -->
        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;
    }
}

还有我的activity.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite">

    <android.support.design.widget.BottomNavigationView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:design="http://schema.android.com/apk/res/android.support.design"
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="bottom"
        design:menu="@menu/menu_main" />
</android.support.design.widget.CoordinatorLayout>

我也更新了gradle版本到25,不然不行。

compileSdkVersion 25
buildToolsVersion "24.0.3"
targetSdkVersion 25

compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'

【问题讨论】:

    标签: android material-design bottom-type


    【解决方案1】:

    它不起作用的原因是 menu 位于错误的命名空间 (design)。请改用app 命名空间。

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorWhite">
    
        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_gravity="bottom"
            app:menu="@menu/menu_main" />
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

      【解决方案2】:

      试试下面的xml。不要忘记添加app:layout_anchor 和app:layout_anchorGravity="bottom"。此处,BottomNavigationView 以重力bottom 锚定到FrameLayout。

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.design.widget.CoordinatorLayout
      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:id="@+id/activity_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:context=".MainActivity">
      
      <FrameLayout
          android:id="@+id/rv"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
          <!-- Contents -->
      </FrameLayout>
      
      <android.support.design.widget.BottomNavigationView
          android:id="@+id/nm_bottom"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/colorPrimaryDark"
          android:foregroundTint="@color/colorAccent"
          app:itemIconTint="@android:color/white"
          app:itemTextColor="@android:color/white"
          app:layout_anchor="@+id/rv"
          app:layout_anchorGravity="bottom"
          app:menu="@menu/nav_menu" />
      
      </android.support.design.widget.CoordinatorLayout>
      

      【讨论】:

        【解决方案3】:

        我遇到了这个问题,因为我的 BottomNavigationView 没有显示在 android studio 的设计中。但是当我运行代码时,它出现在我的手机上。

        所以如果你没有尝试运行它。在搜索答案之前运行它并检查可能是你的问题

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-12-22
          • 2015-12-14
          • 1970-01-01
          • 1970-01-01
          • 2020-10-26
          • 2017-05-01
          • 1970-01-01
          相关资源
          最近更新 更多