Android底部导航栏

时间:2017-04-02 16:36:48

标签: android navbar

我是Android应用程序开发的新手,也是制作我的第一个应用程序的过程。

我正在尝试创建一个底部导航栏。我已经创建了实际的导航栏,但我现在想要它,所以当我点击该图标时,它将转到另一个页面。我目前已经拥有它所以它会显示一些文字说你点击了这个按钮等但我不确定如何制作它以便它会转到另一页。

这是我目前的代码

public class MainActivity extends AppCompatActivity {

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



    BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_add:
                    Toast.makeText(MainActivity.this, "Action Add Clicked", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.action_exercise:


                    Toast.makeText(MainActivity .this, "Action Exercise Clicked", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.action_timer:
                    Toast.makeText(MainActivity.this, "Action Timer Clicked", Toast.LENGTH_SHORT).show();
                    break;
            }
            return true;

        }
    });

}

}

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gymapp.HomePage">


<ImageView
    android:id="@+id/imageView"
    android:layout_width="318dp"
    android:layout_height="198dp"
    app:srcCompat="@drawable/getinshape"
    tools:layout_editor_absoluteX="33dp"
    tools:layout_editor_absoluteY="16dp"
    tools:ignore="ContentDescription" />

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@drawable/nav_item_color_state"
    app:itemTextColor="@drawable/nav_item_color_state"
    app:menu="@menu/bottom_nav_bar"
    />


</RelativeLayout>

这也是我在res / menu

中使用的代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/action_add"
    android:enabled="true"
    android:icon="@drawable/ic_home"
    android:title="Home"
    android:showAsAction="ifRoom"
    />

<item
    android:id="@+id/action_exercise"
    android:enabled="true"
    android:icon="@drawable/exercise"
    android:title="Exercise"
    android:showAsAction="ifRoom"
    />

<item
    android:id="@+id/action_timer"
    android:enabled="true"
    android:icon="@drawable/ic_timer"
    android:title="Timer"
    android:showAsAction="ifRoom"
    />

</menu>

0 个答案:

没有答案
相关问题