手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android实现原生侧滑菜单的超简单方式
Android实现原生侧滑菜单的超简单方式
摘要:先来看看效果图当你点击菜单可以更改图标,例如点击happy,首页就会变一个笑脸,这个实现的过程超级简单你需要使用ToolBar与Drawab...

先来看看效果图

Android实现原生侧滑菜单的超简单方式1

当你点击菜单可以更改图标,例如点击happy,首页就会变一个笑脸,这个实现的过程超级简单

你需要使用ToolBar与DrawableLayout两个比较新的控件

首先要写三个xml布局文件,我这里的布局文件是使用了include标签嵌入的,代码如下

headbar_toolbar.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tbHeadBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/red">
 
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/emotion"
android:textColor="@color/white"
android:textSize="16sp" />
 
</android.support.v7.widget.Toolbar>

my_drawablelayout.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dlMenu"
android:layout_width="match_parent"
android:layout_height="match_parent">
 
<LinearLayout
android:id="@+id/llContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical">
 
<ImageView
android:id="@+id/ivContent"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/angry" />
 
</LinearLayout>
 
<!--android:layout_gravity="start"属性使这部分作为侧滑部分-->
<!--一定要放在下面!!!关于控件的层次性如果不知道的同学去百度!哦不去谷歌-->
<LinearLayout
android:id="@+id/llMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/white"
android:orientation="vertical">
 
<!--用于设置菜单项-->
<ListView
android:id="@+id/lvMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null" />
 
</LinearLayout>
 
</android.support.v4.widget.DrawerLayout>

main_activity.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.demo.usher.demo_slidingmenu.MainActivity">
 
<!--头部-->
<include layout="@layout/headbar_toolbar" />
 
<!--主布局-->
<include layout="@layout/my_drawablelayout" />
 
</LinearLayout>

如何应用在java文件中【一个文件搞定】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.demo.usher.demo_slidingmenu;
 
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
 
import java.util.ArrayList;
import java.util.List;
 
import butterknife.BindView;
import butterknife.ButterKnife;
 
public class MainActivity extends AppCompatActivity {
 
@BindView(R.id.tbHeadBar)
Toolbar mTbHeadBar;
 
/*侧滑菜单布局*/
@BindView(R.id.llMenu)
LinearLayout mLlMenu;
 
/*侧滑菜单ListView放置菜单项*/
@BindView(R.id.lvMenu)
ListView mLvMenu;
 
@BindView(R.id.ivContent)
ImageView mIvContent;
 
@BindView(R.id.dlMenu)
DrawerLayout mMyDrawable;
 
ActionBarDrawerToggle mToggle;
 
private List<String> lvMenuList = new ArrayList<String>() {{
add("angry");
add("happy");
add("sad");
add("embarrassed");
}};
 
private List<Integer> imageList = new ArrayList<Integer>() {{
add(R.drawable.angry);
add(R.drawable.happy);
add(R.drawable.sad);
add(R.drawable.embarrassed);
}};
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
 
/*初始化Toolbar与DrawableLayout*/
initToolBarAndDrawableLayout();
 
mLvMenu.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, lvMenuList));
mLvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mIvContent.setImageResource(imageList.get(position));
mMyDrawable.closeDrawers();/*收起抽屉*/
}
});
}
 
private void initToolBarAndDrawableLayout() {
setSupportActionBar(mTbHeadBar);
/*以下俩方法设置返回键可用*/
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/*设置标题文字不可显示*/
getSupportActionBar().setDisplayShowTitleEnabled(false);
 
mToggle = new ActionBarDrawerToggle(this, mMyDrawable, mTbHeadBar, R.string.open, R.string.close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//Toast.makeText(MainActivity.this, R.string.open, Toast.LENGTH_SHORT).show();
}
 
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
//Toast.makeText(MainActivity.this, R.string.close, Toast.LENGTH_SHORT).show();
}
};
/*mMyDrawable.setDrawerListener(mToggle);不推荐*/
mMyDrawable.addDrawerListener(mToggle);
mToggle.syncState();/*同步状态*/
}
}

关于butterknife注解与样式

butterknife直接在gradle文件中配置好如下【缺什么就补什么】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
 
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
 
defaultConfig {
applicationId "com.demo.usher.demo_slidingmenu"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
 
}
 
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
 
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.jakewharton:butterknife:8.4.0'
/*butterknife相关*/
apt 'com.jakewharton:butterknife-compiler:8.4.0'
compile 'com.android.support:support-v4:24.2.0'
}

style【关于返回键的颜色样式等在style文件中修改】

1
2
3
4
5
6
7
8
9
10
11
<resources>
 
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item>
</style>
 
<style name="AppTheme.DrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle">
<item name="color">@android:color/white</item>
</style>
 
</resources>

总结

其实很多时候我们在使用第三方控件的时候往往不知道背后是怎么实现的,使用原生控件可以让我们更好的理解一个交互或者说实现一个功能的原理,有利于做出性能与交互都非常优秀的APP,以上就是这篇文章的全部内容,希望对大家的工作或学习带来一定的帮助。


【Android实现原生侧滑菜单的超简单方式】相关文章:

Android实现Activities之间进行数据传递的方法

Android实现屏幕旋转方法总结

Android实现发短信/彩信功能

Android应用程序的调试

Android支持的媒体格式

android中实现指针滑动的动态效果方法

Android实现原生侧滑菜单的超简单方式

Android编程实现检测当前电源状态的方法

提升Android应用视觉吸引效果的10个UI设计技巧

android实现字体闪烁动画的方法

精品推荐
分类导航