toolbar文本在最右侧_Android studio:toolbar 自定义及右侧滑动

本文介绍了如何在Android Studio中自定义toolbar,将文本放置在右侧,并实现右侧滑动菜单的功能。首先在build.gradle中导入依赖库,然后创建自定义头部布局、中间内容布局和侧拉菜单头部布局。接着在activity_main.xml中添加toolbar和侧拉菜单,最后在MainActivity.java中初始化菜单并设置点击事件,实现侧滑菜单的打开和关闭。
摘要由CSDN通过智能技术生成

无言

1、build.gradle 导入 compile'com.android.support:design:25.3.1'

2、添加自定义头部导航栏app_bar_main.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true"

tools:context="com.maibo.tiziyouxi.MainActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:theme="@style/AppTheme.AppBarOverlay">

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="?attr/colorPrimary"

android:focusable="true"

android:focusableInTouchMode="true"

app:popupTheme="@style/AppTheme.AppBarOverlay">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:layout_gravity="center"

android:text="@string/app_name"

android:textColor="@android:color/white"

android:textSize="20sp"

android:textStyle="bold" />

2、添加中间主题内容app_content.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/content_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

app:layout_behavior="@string/appbar_scrolling_view_behavior"

tools:context="com.maibo.tiziyouxi.NavigationBarActivity"

tools:showIn="@layout/home_toolbar">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_alignParentStart="true"

android:layout_alignParentTop="true"

android:layout_marginLeft="0dp"

android:layout_marginRight="0dp"

android:background="#e8e8e8">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

3、添加侧拉菜单头部 nav_header_main.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="@dimen/nav_header_height"

android:background="@drawable/side_nav_bar"

android:gravity="bottom"

android:orientation="vertical"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:theme="@style/ThemeOverlay.AppCompat.Dark">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:paddingTop="@dimen/nav_header_vertical_spacing"

android:text="用户名:test"

android:textAppearance="@style/TextAppearance.AppCompat.Body1"

android:textColor="#ffffff" />

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="余额:0.0 元"

android:textColor="#ffffff" />

4、在acticity_main.xml 中添加自定义的toolbar 跟 content

layout="@layout/app_bar_main"

android:layout_width="match_parent"

android:layout_height="match_parent" />

android:id="@+id/nav_view"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:layout_gravity="right"

android:fitsSystemWindows="true"

app:headerLayout="@layout/nav_header_main"

app:menu="@menu/activity_main_drawer" />

5、在MainActivity.java 中开始创建侧拉菜单

public DrawerLayout drawer;

protected void onCreate(Bundle savedInstanceState) {

menuInit();// 初始化菜单

}

public void menuInit(){

// toolbar

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

toolbar.setTitle("");

setSupportActionBar(toolbar);

//初始化侧滑菜单

drawer = (DrawerLayout) findViewById(R.id.activity_main);

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(

this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

drawer.setDrawerListener(toggle);

// toggle.syncState(); // 不设置toolbar的navigation按钮

// 点击右侧按钮显示菜单

toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {

@Override

public boolean onMenuItemClick(MenuItem item) {

if (!drawer.isDrawerOpen(Gravity.RIGHT)) {

drawer.openDrawer(Gravity.RIGHT);

}

return false;

}

});

// 选中隐藏右侧菜单

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

@Override

public boolean onNavigationItemSelected(MenuItem item) {

// 子菜单点击隐藏菜单

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.activity_main);

drawer.closeDrawer(Gravity.RIGHT);

// 子菜单点击触发事件

int id = item.getItemId();

if (id == R.id.nav_kjjg) {

}

return true;

}

});

}

// 点击背景隐藏菜单

@Override

public void onBackPressed() {

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.activity_main);

if (drawer.isDrawerOpen(Gravity.RIGHT)) {

drawer.closeDrawer(Gravity.RIGHT);// 从右侧回收

} else {

super.onBackPressed();

}

}

// 添加菜单按钮

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值