记录每天所学的东西,也当作笔记
1.Material Design ToolBar的使用
首先,在values创建colors.xml中定义三个颜色,colorPrimary,colorPrimary,colorAccent具体显示效果
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#795548</color>
<color name="colorPrimaryDark">#5D4037</color>
<color name="colorAccent">#ffeb3b</color>
</resources>
我这里用的是Brown主题,具体选择可以根据这里 material palette,很不错的配色方案
接下来在style中应用该颜色
<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>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
app:前缀的属性是指兼容非5.0系统下的属性,就是如果你的系统是5.0以上的,可以换成android:
minHeight采用系统的actionbar的高度
背景色是采用主题中的colorPrimary就是一开始定义的,这里的 ?attr/colorPrimary 根据你的Activity的theme而定,如果没有定义,则看app的theme
popupTheme指的是弹出菜单的主题
然后将该toolbar添加到activity_layout.xml中去
<RelativeLayout 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"
tools:context=".MainActivity">
<include
layout="@layout/toolbar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">
</RelativeLayout>
</RelativeLayout>
通过<include />来添加,其中下面再添加一个relativelayout用来显示内容,注意主要属性marginTop,如果没有这个,将会全屏toolbar
最后,只要在MainActivity中把toolbar用上即可,声明
private Toolbar toolbar;
在oncreate()中使用toolbar
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
2.返回导航栏的使用
在第二个Activity中oncreate()方法中添加以下代码,表示显示返回按钮并使用
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);<span style="color:#cc7832">
</span>
getSupportActionBar() 这里返回的是Toolbar
然后再处理按钮事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
最后要记得在配置文件中为它设置父Activity
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
3.RecyclerView的使用
首先在添加该组建的库文件,build.gradle中最后添加,同步下project
compile 'com.android.support:recyclerview-v7:22.2.1'
布局文件中添加组件
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
然后在java代码中初始化必须的组件
private RecyclerView recyclerView;
private List<String> datas;
private MyAdapter adaper;
private int[] res = {
R.drawable.img_1,
R.drawable.img_2,
R.drawable.img_3,
R.drawable.img_4,
R.drawable.img_5
};
class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
LayoutInflater inflater;
Context context;
List<String> data;
public MyAdapter(Context c, List<String> list) {
context = c;
data = list;
inflater = LayoutInflater.from(c);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) {
View view = inflater.inflate(R.layout.text_layout, parent, false);
view.setOnClickListener(new MyOnClickListener());
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int i) {
holder.tv.setText(data.get(i));
holder.iv.setImageResource(res[i % 5]);
}
@Override
public int getItemCount() {
return data.size();
}
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv;
ImageView iv;
public MyViewHolder(View itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(R.id.my_text);
iv = (ImageView) itemView.findViewById(R.id.my_img);
}
}
最后在onCreate()方法中使用
adaper = new MyAdapter(this, datas);
recyclerView.setAdapter(adaper);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
完成
4.Quick Return Pattern
主要修改在于布局
首先添加Design库
compile 'com.android.support:design:22.2.1'
然后修改其中的XML
<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
注意其中的俩个属性
app:layout_scrollFlags="scroll|enterAlways"
scroll属性确定视图可以滚动,而enterAlways确定quick return pattern模式
app:layout_behavior="@string/appbar_scrolling_view_behavior"
这个属性告诉AppBar去响应recyclerView的滚动