高级UI之符合MD的常用控件

在Google提供的控件中,在support-design及v4,v7包中,存在着很多符合MD标准的控件,这里罗列出一些常用的控件

TextInputLayout

这个控件在作为输入框的时候是极其方便及好用的,结合EditText使用

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
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="10">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
</android.support.design.widget.TextInputLayout>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />

</LinearLayout>

效果

SearchView

搜索功能,位于ActionBar的位置
首先需要编写menu布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">

<item android:id="@+id/menu_search"
android:orderInCategory="100"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="search"/>
<item android:id="@+id/menu_share"
android:orderInCategory="100"
app:showAsAction="ifRoom"
android:icon="@android:drawable/ic_menu_share"
android:title="share" />
</menu>

然后复写onCreateOptionsMenu()方法

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
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem menuItem = menu.findItem(R.id.menu_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
//显示搜索框
//searchView.setIconified(false);
//显示搜索框,且其不能被隐藏
searchView.setIconifiedByDefault(false);
//显示提交按钮,这里可以获取到id,设置自定义图片
ImageView icon = (ImageView) searchView.findViewById(R.id.search_go_btn);
icon.setImageResource(R.mipmap.ic_launcher);
icon.setVisibility(View.VISIBLE);
//设置监听
icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, searchView.getQuery(), Toast.LENGTH_SHORT).show();
}
});
//获取id,从而设置提示
EditText edit = (EditText) searchView.findViewById(R.id.search_src_text);
edit.setHint("请输入搜索的内容");

searchView.setSubmitButtonEnabled(true);
//SearchView还有很多设置监听选项,例如提交监听,文本监听

return true;
}
}

效果如下:

Toolbar

顶部导航栏。用于显示标题,返回,菜单等,最开始是使用的ActionBar
由于ActionBar的种种使用不便,再加上拓展性差,google后来推出了Toolbar,为了增强其功能,现在还有APPbar可供使用
首先还是要把主题设置为NoActionBar

1
2
3
4
5
6
7
8
9
10
11
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

然后使用Toolbar布局,由于ToolBar继承自ViewGroup,所以其是一个容器

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
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:logo="@mipmap/ic_launcher"
app:navigationIcon="@drawable/abc_ic_ab_back_material"
app:subtitle="子标题"
app:subtitleTextColor="@android:color/white"
app:title="主标题"
app:titleTextColor="@color/colorAccent">
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="center"-->
<!--android:text="这里是标题"/>-->
</android.support.v7.widget.Toolbar>

</LinearLayout>

最后设置Toolbar到活动,并设置监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}

测试效果如下

接下来实现Toolbar的隐藏效果
这样的效果实现思路就是Toolbar在上层,后面的布局在下层,后面的布局设置padding,然后再滑动过程中监听滑动距离,设置Toolbar的透明度

1
2
3
4
interface TranslucentListener {
//透明度的回调监听 alpha:0~1
void onTranlucent(float alpha);
}

自定义ScrollView,在onScrollChanged()中监听

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
public class MyScrollView extends ScrollView {
private TranslucentListener translucentListener;

public void setTranslucentListener(TranslucentListener translucentListener) {
this.translucentListener = translucentListener;
}

public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (translucentListener != null) {
Log.d("cj5785", "onScrollChanged");
int scrollY = getScrollY();
int screen_height = getContext().getResources().getDisplayMetrics().heightPixels;
//滑出1/3时候完全透明
if (scrollY <= screen_height / 3f) {
translucentListener.onTranlucent(1 - scrollY / (screen_height / 3f));
}
}
}
}

最后在活动中回调

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
public class MainActivity extends AppCompatActivity implements TranslucentListener{

private Toolbar toolbar;
private MyScrollView scrollView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
scrollView = findViewById(R.id.scroll_view);
scrollView.setTranslucentListener(this);
}

@Override
public void onTranlucent(float alpha) {
toolbar.setAlpha(alpha);
}
}

贴出布局
android:clipToPadding="false" 该控件的绘制范围是否不在Padding里面。false:绘制的时候范围会考虑padding即会往里面缩进
android:clipChildren="false" 子控件是否能不超出padding的区域(比如ScrollView上滑动的时候,child可以滑出该区域)

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
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.cj5785.toolbartest.MyScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:paddingTop="?attr/actionBarSize">

<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/holo_orange_light" />

<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/holo_green_light" />

<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/holo_blue_light" />

<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/holo_red_light" />

<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/holo_purple" />


</android.support.v7.widget.LinearLayoutCompat>
</com.cj5785.toolbartest.MyScrollView>

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:logo="@mipmap/ic_launcher"
app:navigationIcon="@drawable/abc_ic_ab_back_material"
app:subtitle="子标题"
app:subtitleTextColor="@android:color/white"
app:title="主标题"
app:titleTextColor="@color/colorAccent">
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="center"-->
<!--android:text="这里是标题"/>-->
</android.support.v7.widget.Toolbar>

</RelativeLayout>

实现效果:

CoordinatorLayout

监听滑动控件的滑动通过Behavior反馈到其他子控件并执行一些动画
这里的滑动控件指的是RecyclerView/NestedScrollView/ViewPager,意味着ListViewScrollView不行
使用前需要添加support-design依赖

1
implementation 'com.android.support:design:25.4.0'

布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<android.support.design.widget.FloatingActionButton
app:layout_behavior=".FABBehavior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp" />

</android.support.design.widget.CoordinatorLayout>

适配器

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
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

private List<String> list;

class MyViewHolder extends RecyclerView.ViewHolder {
private TextView textView;

public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(android.R.id.text1);
}
}

public MyAdapter(List<String> list) {
this.list = list;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(android.R.layout.simple_list_item_1,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.textView.setText(list.get(position));
}

@Override
public int getItemCount() {
return list.size();
}
}

Behavior

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
public class FABBehavior extends FloatingActionButton.Behavior {

private boolean isShow;

public FABBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}

//依赖滑动开始回调
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
//nestedScrollAxes 滑动的方向,这里我们依赖recyclerview,只关心其是否垂直滑动
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
|| super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
}

//依赖滑动过程回调
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
//根据情况执行动画
if (dyConsumed > 0 && isShow) {
isShow = false;
onHide(child);
} else if (dyConsumed < 0) {
isShow = true;
onShow(child);
}
}

private void onShow(FloatingActionButton fab) {
ViewCompat.animate(fab).scaleX(1f).scaleY(1f).start();
}

private void onHide(FloatingActionButton fab) {
ViewCompat.animate(fab).scaleX(0f).scaleY(0f).start();
}
}

调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MainActivity extends AppCompatActivity {
private List<String> list = new ArrayList<>();
private RecyclerView recyclerView;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
for (int i = 0; i < 100; i++) {
list.add("item " + i);
}
adapter = new MyAdapter(list);
recyclerView.setAdapter(adapter);
}
}

效果如下,实现了FloatingActionButton上划隐藏,下拉显示

AppBarLayout

AppBarLayout继承自LinearLayout,一般用于导航栏,其常见子控件为Toolbar,但同时又不局限于Toolbar,可以实现导航栏的多种综合效果,其作为容器,里面可以增加布局,按照需要去实现各种效果
这里做一个简单的演示,导航栏通过recyclerview的滑动而显隐的效果
首先依旧是要引入依赖

1
implementation 'com.android.support:design:25.4.0'

然后编写布局,其外层为CoordinatorLayout,这里使用了预设behavior:appbar_scrolling_view_behavior,在监听的控件加上Flag设置,例如这里的app:layout_scrollFlags="scroll|enterAlways""
其Flag参数包括
scroll:将此布局和滚动时间关联。这个标识要设置在其他标识之前,没有这个标识则布局不会滚动且其他标识设置无效
enterAlways:任何向下滚动操作都会使此布局可见。这个标识通常被称为快速返回模式
enterAlwaysCollapsed:假设你定义了一个最小高度(minHeight)同时enterAlways也定义了,那么view将在到达这个最小高度的时候开始显示,并且从这个时候开始慢慢展开,当滚动到顶部的时候展开完
exitUntilCollapsed:当你定义了一个minHeight,此布局将在滚动到达这个最小高度的时候折叠
snap:当一个滚动事件结束,如果视图是部分可见的,那么它将被滚动到收缩或展开。例如,如果视图只有底部25%显示,它将折叠。相反,如果它的底部75%可见,那么它将完全展开
snap:

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
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="center"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:title="这是标题" />
</android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

由于使用的是RecyclerView,故还需要适配器

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
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<String> list;

class MyViewHolder extends RecyclerView.ViewHolder {
private TextView textView;

public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(android.R.id.text1);
}
}

public MyAdapter(List<String> list) {
this.list = list;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(android.R.layout.simple_list_item_1,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.textView.setText(list.get(position));
}

@Override
public int getItemCount() {
return list.size();
}
}

设置RecyclerView数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class MainActivity extends AppCompatActivity {
private List<String> list = new ArrayList<>();
private MyAdapter adapter;
private RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
for (int i = 0; i < 30; i++) {
list.add("item " + i);
}
adapter = new MyAdapter(list);
recyclerView.setAdapter(adapter);
}
}

查看效果如下

ViewPager + TabLayout + Fragment + AppBarLayout

AppBarLayout同时还可以使用其他可滑动控件,例如NestedScrollView,其常用组合为:ViewPager + TabLayout + Fragment + AppBarLayout
由于这套组合的体验效果不错,这里展现一下这个样式的demo
首先依旧是引入依赖

1
2


因为使用了Toolbar,所以这里使用NoActionBar的主题

1
2
3
4
5
6
7
8
9
10
11
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

编写ViewPager的Fragment布局,这里简单显示text,记得这里应该是一个滑动控件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@android:color/black"
android:textSize="24sp" />
</android.support.v4.widget.NestedScrollView>
</LinearLayout>

然后是自定义的fragment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class DefaultFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout,container,false);
TextView textView = (TextView) view.findViewById(R.id.text_view);
Bundle bundle = getArguments();
String title = bundle.getString("title");
textView.setText(title+"\n\n");
for (int i = 0; i < 10; i++) {
textView.append("这是一个ViewPager + TabLayout + Fragment + AppBarLayout测试用例\n\n");
}
return view;
}
}

接下来是主布局

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
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:title="标题" />

<android.support.design.widget.TabLayout
android:id="@+id/table_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_orange_light"
app:tabGravity="fill"
app:tabMode="scrollable" />
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

最后关联TableLayout和ViewPager

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
public class MainActivity extends AppCompatActivity {

private TabLayout tabLayout;
private ViewPager viewPager;
private String[] title = {
"新闻", "体育", "汽车", "科技", "手机", "数码", "读书", "艺术"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout) findViewById(R.id.table_layout);
viewPager = (ViewPager) findViewById(R.id.view_pager);
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}

class MyPagerAdapter extends FragmentPagerAdapter {

public MyPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public CharSequence getPageTitle(int position) {
return title[position];
}

@Override
public Fragment getItem(int position) {
Fragment fragment = new DefaultFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title[position]);
fragment.setArguments(bundle);
return fragment;
}

@Override
public int getCount() {
return title.length;
}
}
}

这样就完成了显示功能

CollapsingToolbarLayout

这个控件可以实现Toolbar的折叠效果
先看下其常用参数
expandedTitleMargin:展开后的边距
statusBarScrim:状态栏颜色
contentScrim:内容颜色
app:layout_collapseMode:设置折叠模式,分为视差模式(parallax:在折叠的时候会有折叠视差效果,一般搭配layout_collapseParallaxMultiplier,视差的明显程度),固定模式(pin:在折叠的时候最后固定在顶端,再和toolbar一起推出),无模式(none:折叠时toolbar直接推出)
expandedTitleGravity:展开的位置
collapsedTitleGravity:折叠的位置
使用时依然需要导入依赖

1
implementation 'com.android.support:design:25.4.0'

然后使用NoActionBar主题
使用时的布局示例

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
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/test_text"
android:textColor="@android:color/black"
android:textSize="24sp" />
</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="180dp">

<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:expandedTitleMargin="8dp"
app:statusBarScrim="?attr/colorPrimary"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleGravity="center"
app:collapsedTitleGravity="center"
app:title="现代诗歌欣赏">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/tdj"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.5"/>

<android.support.v7.widget.Toolbar
app:layout_collapseMode="pin"
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

设置Toolbar

1
2
3
4
5
6
7
8
9
10
11
12
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
toolbar.setNavigationIcon(R.mipmap.ic_launcher);
setSupportActionBar(toolbar);
}
}

实现的效果如下图

Donate comment here