Androidについて話しましょう(第261章:AndroidのBottomNavigationView 1)

みなさん、こんにちは。前回AndroidでのBottomNavigationViewの例についてお話しましたが、今回は引き続き例についてお話します。ゴシップについて話すのをやめて、ビジネスに戻ってください。一緒にAndroidと話そう!

BottomNavigationView関係者を見てください。前の章で概要を説明しました。この章では、BottomNavigationViewコントロールの使用方法を紹介します。参照用の具体的な手順は次のとおりです。

  • 1.依存ライブラリを追加し、aapp /build.gradleファイルに追加します。
 dependencies {
    
    
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation 'junit:junit:4.12'
    //添加内容如下
    implementation 'com.android.support:design:28.0.0'
}
  • 2.レイアウトファイルでこのコントロールを使用するには、ここで完全なパッケージ名を使用する必要があります。
 <android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_marginBottom="1dp"
    android:background="?android:attr/windowBackground"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp"
    app:menu="@menu/navigation" />
  • 3.プロジェクトにメニューリソースを作成し、app:menu属性を使用してメニューをコントロールに追加します(上記のコードの最後の行を参照してください)。
<?xml version = "1.0" encoding = "utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_dashboard" />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_notifications" />

</menu>

メニューのタイトルタグとアイコンタグは、主に上記のコードで使用されます。ナビゲーションメニューにアイコンとテキストを表示するために使用されますが、アイコン自体の色を使用する代わりに、デフォルトの白黒が使用されます。itemIconTintプロパティとitemTextColorプロパティを使用して、アイコンとテキストの色を制御できます。

みなさん、ここでAndroidのBottomNavigationViewの例を紹介しましょう。他の例を知りたい場合は、次回も聴いてみましょう。

おすすめ

転載: blog.csdn.net/talk_8/article/details/107440058