<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“180dp”
android:background=“@color/colorPrimary”
android:padding=“10dp”>
<de.hdodenhof.circleimageview.CircleImageView
android:id=“@+id/icon_image”
android:layout_width=“70dp”
android:layout_height=“70dp”
android:layout_centerInParent=“true”
android:src=“@drawable/nav_icon” />
<TextView
android:id=“@+id/tv_mail”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentBottom=“true”
android:textColor=“#FFF”
android:textSize=“14sp”
android:text=“xfhy@gmail.com”/>
<TextView
android:id=“@+id/tv_username”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_above=“@id/tv_mail”
android:textSize=“14sp”
android:textColor=“#FFF”
android:text=“Tony Gonm”/>
高度设为180dp,这是一个NavigationView比较适合的高度.
3.然后将布局中滑动菜单布局替换一下,具体实现:
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
android:id=“@+id/drawer_layout”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<FrameLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<android.support.v7.widget.Toolbar
android:id=“@+id/toolbar”
android:layout_width=“match_parent”
android:layout_height=“?attr/actionBarSize”
android:background=“?attr/colorPrimary”
android:theme=“@style/ThemeOverlay.AppCompat.Dark.ActionBar”
app:popupTheme=“@style/ThemeOverlay.AppCompat.Light” />
<android.support.design.widget.NavigationView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:id=“@+id/nav_view”
android:layout_gravity=“start”
app:menu=“@menu/nav_menu”
app:headerLayout=“@layout/nav_header”/>
</android.support.v4.widget.DrawerLayout>
4.在Activity中找到NavigationView控件
navView.setCheckedItem(R.id.nav_call); //设置默认选中call选项
//设置NavigationView菜单选中事件的监听器
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//这里可以根据item的getItemId()来判断 具体点击了哪个选项
mDrawerLayout.closeDrawers(); //关闭滑动菜单
return true;
}
});
3. 悬浮按钮和可交互提示
==============
3.1 FloatingActionButton
FloatingActionButton是Design Support 库中提供的一个控件,这个控件可以帮助我们轻松地实现悬浮按钮的效果.
1.首先,在上面的基础上进行修改activity_main.xml布局.将下面这段代码放到FrameLayout中.
<android.support.design.widget.FloatingActionButton
android:id=“@+id/fab_done”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“bottom|end”
android:layout_margin=“16dp”
android:src=“@drawable/ic_done” />
2.设置点击事件,和普通按钮一样的用法.
FloatingActionButton fab_done = (FloatingActionButton) findViewById(R.id.fab_done);
//设置点击事件 和普通按钮的点击事件是一样的
fab_done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, “你点我了(Done)”, Toast.LENGTH_SHORT).show();
}
});
3.2 Snackbar
Design Support库中提供的更加先进的提示工具—-Snackbar
首先要明确,Snackbar并不是Toast的替代品,它们两者之间有着不同的应用场景.
Snackbar允许在提示当中加入一个可交互按钮,当用户点击按钮的时候可以执行一些额外的逻辑操作.
过一段时间会自动消失
效果如下:
用法如下:
//需要传入View对象 这个view只要是当前界面布局的任意一个View都可以,Snackbar会根据这个View来
//自动查找最外层的布局,用来展示Snackbar.
Snackbar.make(view,“Data Deleted”,Snackbar.LENGTH_SHORT)
.setAction(“Undo”,new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, “Data restored”, Toast.LENGTH_SHORT).show();
}
})
.show();
存在的问题:这个Snackbar竟然将我们的悬浮按钮给遮挡住了.
3.3 CoordinatorLayout
上面遗留的问题用CoordinatorLayout就可以轻松解决.CoordinatorLayout可以说是一个加强版的FrameLayout,这个布局也是由Design Support库提供的.
事实上,CoordinatorLayout可以监听其所有子控件的各种事件,然后自动帮助我们做出最合理的响应.
如下:
用法
直接替换用android.support.design.widget.CoordinatorLayout
替换掉上面的FrameLayout
4 卡片式布局
=======
4.1 CardView
CardView是用于实现卡片式布局效果的重要控件,由appcompat-v7库提供.实际上,CardView就是一个FrameLayout,只是额外提供了圆角和阴影等效果,看上去会有立体的效果. 下面就将使用CardView作为Recycler的子项来使用,达到下面图片上的效果.
效果如下:
1.首先需要往app/build.gradle文件中声明这些库的依赖才行
compile 'com.android.support:cardview-v7:24.2.1' //CardView
compile 'com.android.support:recyclerview-v7:24.2.1' //RecyclerView
compile 'com.github.bumptech.glide:glide:3.7.0' //强大的图片加载库
2.然后每个RecyclerView的子项就是用CardView来实现的.具体代码:
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView 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=“wrap_content”
android:layout_margin=“5dp”
app:cardCornerRadius=“4dp”>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“vertical”>
<ImageView
android:id=“@+id/fruit_image”
android:layout_width=“match_parent”
android:layout_height=“100dp”
android:scaleType=“centerCrop” />
<TextView
android:id=“@+id/fruit_name”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center_horizontal”
android:layout_margin=“5dp”
android:textSize=“16sp” />
</android.support.v7.widget.CardView>
3.然后直接将RecyclerView显示出来即可 就可以看到CardView的效果了.
上面的那种2列的效果是配合
GridLayoutManager layoutManager = new GridLayoutManager(this,2); //2列 recyclerView.setLayoutManager(layoutManager);
这个使用的.
待解决问题:RecyclerView把Toolbar遮盖住了.
4.2 AppBarLayout
AppBarLayout是Design Support提供的另一个工具,AppBarLayout实际上是一个垂直方向的LinearLayout,它在内部做了很多滚动事件的封装,并应用了一些Material Design的设计理念.
AppBarLayout必须是CoordinatorLayout的子布局
可以实现的效果:当往下滑的时候标题栏自动隐藏,往上滑的时候标题栏用重新出现.
- 首先将Toolbar用AppBarLayout包起来:
<android.support.design.widget.AppBarLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”>
<android.support.v7.widget.Toolbar
android:id=“@+id/toolbar”
android:layout_width=“match_parent”
android:layout_height=“?attr/actionBarSize”
android:background=“?attr/colorPrimary”
android:theme=“@style/ThemeOverlay.AppCompat.Dark.ActionBar”
app:layout_scrollFlags=“scroll|enterAlways|snap”
app:popupTheme=“@style/ThemeOverlay.AppCompat.Light” />
</android.support.design.widget.AppBarLayout>
- 然后将布局中的RecyclerView加一个属性,这个属性可以指定布局的行为.
app:layout_behavior=“@string/appbar_scrolling_view_behavior”
- 现在往Toolbar中添加一个
app:layout_scrollFlags="scroll|enterAlways|snap"
属性,并将这个属性的值指定成了scroll|enterAlways|snap.其中,scroll表示当RecyclerView向上滚动的时候,Toolbar会跟着一起向上滚动并实现隐藏;enterAlways 表示当RecyclerView向下滚动的时候Toolbar会跟着一起向下滚动并重新显示.snap表示当Toolbar还没有完全隐藏或显示的时候,会根据当前滚动的距离,自动选择是隐藏还是显示.
4.3 下拉刷新
SwipeRefreshLayout就是用于实现下拉刷新功能的核心类,它是由support-v4库提供的.我们把想要实现下拉刷新功能的控件放置到SwipeRefreshLayout中,就可以迅速让这个控件支持下拉刷新.
效果如下:
使用方法
- 在布局中这样用
<android.support.v4.widget.SwipeRefreshLayout
android:id=“@+id/swipe_refresh”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
app:layout_behavior=“@string/appbar_scrolling_view_behavior”>
<android.support.v7.widget.RecyclerView
android:id=“@+id/recycler_view”
android:layout_width=“match_parent”
android:layout_height=“match_parent” />
</android.support.v4.widget.SwipeRefreshLayout>
- 在Activity中,可以设置监听器等
private SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary); //设置下拉刷新进度条的颜色
//设置下拉刷新的监听器
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshFruits(); //进行刷新操作
}
});
5. 可折叠式标题栏
===========
5.1 CollapsingToolbarLayout
实现一个可折叠式标题栏的效果,需要借助CollapsingToolbarLayout这个工具.
效果如下:
页面上有三部分,水果标题栏,水果内容详情和悬浮按钮
- 在布局中这样写:
<android.support.design.widget.CoordinatorLayout 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:id=“@+id/activity_fruit”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“com.xfhy.materialtest.FruitActivity”>
<android.support.design.widget.AppBarLayout
android:id=“@+id/appBar”
android:layout_width=“match_parent”
android:layout_height=“250dp”>
<android.support.design.widget.CollapsingToolbarLayout
android:id=“@+id/collapsing_toolbar”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:theme=“@style/ThemeOverlay.AppCompat.Dark.ActionBar”
app:contentScrim=“?attr/colorPrimary”
app:layout_scrollFlags=“scroll|exitUntilCollapsed”>
<ImageView
android:id=“@+id/fruit_image_view”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:scaleType=“centerCrop”
app:layout_collapseMode=“parallax” />
<android.support.v7.widget.Toolbar
android:id=“@+id/toolbar”
android:layout_width=“match_parent”
android:layout_height=“?attr/actionBarSize”
app:layout_collapseMode=“pin”>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
app:layout_behavior=“@string/appbar_scrolling_view_behavior”>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“vertical”>
<android.support.v7.widget.CardView
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginBottom=“15dp”
android:layout_marginLeft=“15dp”
android:layout_marginRight=“15dp”
android:layout_marginTop=“34dp”
app:cardCornerRadius=“4dp”>
<TextView
android:id=“@+id/fruit_content_text”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_margin=“10dp” />
</android.support.v7.widget.CardView>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_margin=“16dp”
android:src=“@drawable/ic_comment”
app:layout_anchor=“@id/appBar”
app:layout_anchorGravity=“bottom|end” />
</android.support.design.widget.CoordinatorLayout>
- 然后需要在Activity中让导航图标可见,并且生成水果介绍的数据.
package com.xfhy.materialtest;
public class FruitActivity extends AppCompatActivity {
public static final String FRUIT_NAME = “fruit_name”;
public static final String FRUIT_IMAGE_ID = “fruit_image_id”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fruit);
Intent intent = getIntent();
String fruitName = intent.getStringExtra(FRUIT_NAME);
int fruitImageId = intent.getIntExtra(FRUIT_IMAGE_ID, 0);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout)
findViewById(R.id.collapsing_toolbar);
ImageView fruit_image_view = (ImageView) findViewById(R.id.fruit_image_view);
TextView fruit_content_text = (TextView) findViewById(R.id.fruit_content_text);
setSupportActionBar(toolbar); //设置标题栏
ActionBar actionBar = getSupportActionBar();
if(actionBar != null){
actionBar.setDisplayHomeAsUpEnabled(true); //设置导航图标可见
}
collapsingToolbar.setTitle(fruitName); //设置标题栏 标题
Glide.with(this).load(fruitImageId).into(fruit_image_view); //设置显示水果图片
String fruitContent = generateFruitContent(fruitName);
fruit_content_text.setText(fruitContent); //设置水果介绍需要显示的文字
}
/**
-
生成水果的介绍
-
@param fruitName
-
@return
*/
private String generateFruitContent(String fruitName) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 500; i++) {
sb.append(fruitName);
}
return sb.toString();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home: //这是导航按钮的id(固定值)
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
- 当然,如果需要设置卡片的点击事件,点击后跳转到当前的水果介绍页面则需要在FruitAdapter中设置点击事件
//创建ViewHolder实例
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (mContext == null) {
mContext = parent.getContext();
}
//加载布局到view
View view = LayoutInflater.from(mContext).inflate(R.layout.fruit_item, parent, false);
final ViewHolder viewHolder = new ViewHolder(view);
//设置每个卡片的点击事件
viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int adapterPosition = viewHolder.getAdapterPosition();
Fruit fruit = mFruitList.get(adapterPosition);
Intent intent = new Intent(mContext, FruitActivity.class);
intent.putExtra(FruitActivity.FRUIT_NAME, fruit.getName());
intent.putExtra(FruitActivity.FRUIT_IMAGE_ID, fruit.getImageId());
mContext.startActivity(intent);
}
});
return viewHolder; //创建ViewHolder实例
}
5.2 充分利用系统状态栏空间(感觉像是沉浸式状态栏)
上面的效果还有个缺陷,背景图片和系统的状态栏总有一些不搭的感觉.只不过可惜的是,在Android 5.0 之前,我们是无法对状态栏的背景或颜色进行操作的,那个时候也还没有Material Design的概念.
想要让背景图能够和状态栏融合,需要借助android:fitsSystemWindows这个属性来实现.
效果如下:
1.在CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout,ImageView,这种嵌套结构的布局中,将android:fitsSystemWindows属性指定成true;
2.还需要在程序的主题中将状态栏的颜色指定成透明色才行.指定成透明色的方法:在主题中将android:statusBarColor属性的值指定成@android:color/transparent就可以了. 但是有个问题,android:statusBarColor属性是从API 21 开始的,也就是Android 5.0 系统才开始有的.那么,系统差异型的功能实现就要从这里开始了.
在res下创建目录values-21,然后在该目录下创建values resources file ,写入如下代码:
<?xml version="1.0" encoding="utf-8"?>但是,在5.0 之前的系统无法识别FruitActivityTheme这个主题,所以需要在res->values->styles.xml添加2行代码,中间什么都没有.
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
![](https://i-blog.csdnimg.cn/blog_migrate/4559172899c2eec6dc1a6db02b160df6.jpeg)
结尾
好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。
这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家~
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
但是,在5.0 之前的系统无法识别FruitActivityTheme这个主题,所以需要在res->values->styles.xml添加2行代码,中间什么都没有.
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-wbmcoNb6-1712665813118)]
[外链图片转存中…(img-cxNPghRE-1712665813119)]
[外链图片转存中…(img-MEKE6Nft-1712665813119)]
[外链图片转存中…(img-t23ChOiw-1712665813119)]
[外链图片转存中…(img-9pMxYslG-1712665813119)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
![](https://i-blog.csdnimg.cn/blog_migrate/4559172899c2eec6dc1a6db02b160df6.jpeg)
结尾
好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。
这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家~
[外链图片转存中…(img-3Uaro21y-1712665813119)]
[外链图片转存中…(img-XoakBM9c-1712665813120)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!