1.Toolbar和DrawerLayout实现
Toolbar:标题栏
DrawerLayout:可以实现侧滑
2.SlideMenu实现 第三方 需要导入moudle(slidemenu)
一.ToolBar常用的方法
Toolbar是在 Android 5.0 开始推出的一个 Material Design 风格的导航控件,以此来取代之前的Actionbar 。我们需要在工程中引入appcompat-v7的兼容包以便向下兼容, 使用android.support.v7.widget.Toolbar进行开发。在设计 Toolbar 的时候,Google也留给了开发者很多可定制修改的余地,这些可定制修改的属性在API文档中都有详细介绍,如:
1.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);去掉标题栏;
2.Toolbar.setLogo(),设置logo图片;
3.Toolbar.setTitle().设置标题;
4.Toolbar.setSubTitle()设置子标题;
5.Toolbar.setTitleTextColor(int color);设置标题文字颜色;
6.Toolbar.setSubtitleTextColor();设置子标题文字颜色;
7.setTitleMargin(int start, int top, int end, int bottom);设置标题margin值; 8.onCreateOptionsMenu,getMenuInflater().inflate(R.menu.menu,menu)
设置菜单在给Toolbar设置为actionbar时使用;
9.Toolbar.setOnMenuItemClickListener();Toolbar绑定menu监听;
10.Toolbar.inflateMenu(R.menu.menu)在Toolbar没有替换actionbar时使用;
11.setSupportActionBar(mToolbar);设置toolbar替换actionbar;
12.getLayoutInflater().inflate(R.layout.view_tv,bar);Toolbar添加自定义view
二.DrawerLayout常用的方法
DrawerLayout.isDrawerOpen(Gravity.LEFT)是否开启;
DrawerLayout.openDrawer(Gravity.LEFT);开启抽屉
DrawerLayout.closeDrawer(Gravity.RIGHT);关闭抽屉
三.ToolBar和DrawerLayout代码
(0)先去掉自带的ActionBar,在清单文件中
(1)布局文件代码:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="@+id/draw"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<LinearLayout
android:background="#ffffff"
android:layout_gravity="left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="个人装饰"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="消息中心"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="免流量服务"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="定时关闭"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="仅Wi_Fi联网"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="流量提醒"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="听歌偏好"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="微云音乐网盘"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清理空间"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="帮助与反馈"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关于QQ音乐"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="精品应用推荐"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
package com.example.administrator.myapplication2;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ImageView imageView;
DrawerLayout drawerLayout;
TextView tv;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=findViewById(R.id.tv);
drawerLayout = findViewById(R.id.draw);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, ""+tv, Toast.LENGTH_SHORT).show();
}
});
// toolbar=findViewById(R.id.toolbar);
imageView=findViewById(R.id.iv);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.openDrawer(Gravity.LEFT);
}
});
}
}
//
package com.example.administrator.myapplication2;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
public class Main3Activity extends AppCompatActivity {
ImageView imageView;
SlidingMenu slidingMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
initView();
initMeu();
}
private void initView() {
imageView=findViewById(R.id.iv);
}
public void initMeu(){
slidingMenu= new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT);
slidingMenu.setBehindOffset(100);
View view = LayoutInflater.from(this).inflate(R.layout.item, null);
view.findViewById(R.id.bt_close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
slidingMenu.showContent();
}
});
view.findViewById(R.id.bt_exit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
slidingMenu.setMenu(view);
slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setShadowWidth(300);
// slidingMenu.setShadowDrawable(R.mipmap.ic_launcher);
slidingMenu.setFadeEnabled(true);
slidingMenu.setFadeDegree(0.1f);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:background="#2196F3"
android:layout_width="match_parent"
android:layout_height="70dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textColor="#FFF"
android:layout_centerInParent="true"
android:textSize="30sp"
android:text="消息"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="@mipmap/ic_launcher_round"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="旧梦"
android:textSize="30dp"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拍摄我的状态"
android:textSize="30dp"
/>
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="了解会员"
android:textSize="30dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="QQ钱包"
android:textSize="30dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="个人装饰"
android:textSize="30dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我的收藏"
android:layout_marginTop="10dp"
android:textSize="30dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我的相册"
android:layout_marginTop="10dp"
android:textSize="30dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我的文件"
android:layout_marginTop="10dp"
android:textSize="30dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="免流量特权"
android:layout_marginTop="10dp"
android:textSize="30dp"/>
<Button
android:text="关闭抽屉"
android:id="@+id/bt_close"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="退出"
android:id="@+id/bt_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
本文详细介绍了在Android应用中如何使用Toolbar和DrawerLayout组件。包括Toolbar的常见用法,如设置标题、Logo、颜色和菜单,以及DrawerLayout的开闭操作。通过具体代码示例,展示了如何集成这两个组件以增强应用的导航功能。

2555

被折叠的 条评论
为什么被折叠?



