2017-04-14 60 views

回答

0

你可以得到那樣的使用底部導航選項卡效果。 試試這個代碼,希望它可以幫助你

對於底部導航選項卡則必須將此行添加到依賴

compile ‘com.android.support:design:25.0.0’ 

這裏是MainActivity.Java

公共類MainActivity擴展AppCompatActivity { 私人BottomBar mBottomBar ;

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Notice how you don't use the setContentView method here! Just 
    // pass your layout to bottom bar, it will be taken care of. 
    // Everything will be just like you're used to. 
    mBottomBar = BottomBar.bind(this, R.layout.activity_main, 
      savedInstanceState); 

    mBottomBar.setItems(
      new BottomBarTab(R.drawable.ic_recents, "Recents"), 
      new BottomBarTab(R.drawable.ic_favorites, "Favorites"), 
      new BottomBarTab(R.drawable.ic_nearby, "Nearby"), 
      new BottomBarTab(R.drawable.ic_friends, "Friends") 
    ); 

    mBottomBar.setOnItemSelectedListener(new OnTabSelectedListener() { 
     @Override 
     public void onItemSelected(final int position) { 
      // the user selected a new tab 
     } 
    }); 
} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    mBottomBar.onSaveInstanceState(outState); 
} 
} 

Design.xml

<?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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- Content Container --> 

    <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="@color/white" 
     app:itemTextColor="@color/white" 
     app:menu="@menu/bottom_navigation_main" /> 

</RelativeLayout> 

menu.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <item 
     android:id="@+id/action_favorites" 
     android:enabled="true" 
     android:icon="@drawable/ic_favorite_white_24dp" 
     android:title="@string/text_favorites" 
     app:showAsAction="ifRoom" /> 
    <item 
     android:id="@+id/action_schedules" 
     android:enabled="true" 
     android:icon="@drawable/ic_access_time_white_24dp" 
     android:title="@string/text_schedules" 
     app:showAsAction="ifRoom" /> 
    <item 
     android:id="@+id/action_music" 
     android:enabled="true" 
     android:icon="@drawable/ic_audiotrack_white_24dp" 
     android:title="@string/text_music" 
     app:showAsAction="ifRoom" /> 
</menu> 

使用該

Enable/Disable選擇狀態

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="@color/white" android:state_enabled="true" /> 
    <item android:color="@color/colorPrimaryDark" android:state_enabled="false" /> 
</selector> 

手柄click事件在佈局

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_favorites: 

         break; 
        case R.id.action_schedules: 

         break; 
        case R.id.action_music: 

         break; 
       } 
       return false; 
      } 
}); 
0

添加視圖

<View 
    android:layout_width="fill_parent" 
    android:layout_height="5dip" 
    android:background="@drawable/drop_shadow" > 
</View> 

添加在您繪製文件夾drop_shadow.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android">  
<gradient 
    android:startColor="#404040" 
    android:endColor="#F1F1F1" 
    android:angle="270" 
    > 
</gradient> 
</shape>