project structure -> dependencies引入失败 sdk目录不能有空格 安装butterknife插件的流程 1.安装插件 2.网络下载butterknife支持包 project structure -> dependencies 3.在project的build.gradle中增加 dependencies { classpath 'com.android.tools.build:gradle:2.2.2' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } 4.在app的build.gradle中增加 apply plugin: 'android-apt'
dependencies {
......
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.1.0'
}
-
自定义状态栏、标题栏、导航栏的颜色 1.1 创建相应的样式文件 右键点击res -> 选择Android resource file -> 设置名称styles,设置存放目录values-v21,设置支持的版本21 1.2 将values目录下的styles.xml文件中的代码拷贝过来,修改样式主题是android:Theme.Material 如果想要使用白色主题使用android:Theme.Material.Light 1.3 手动更改相应的样式 1.4 通过主题编辑页面进行操作 点击上部Open editer选项进入主题编辑页面,可以直观的查看设置的样式 2.切换主题样式 选择Android5.0项目 -> ctrl+shift+F -> read -> StyleFragment.java -> 点击case RED_THEME进入java类中 2.1 找到调用setTheme方法的地方 2.2 在setTheme方法中发现是设置样式MainActivity的Theme 2.3 在调用的地方发现设置完之后,是将MainActivity移除重新打开MainActivity
核心操作:将主题样式设置给activity,将activity关闭,及时重新打开的activity,设置新的样式
-
海拔和阴影 3.1 海拔阴影高度属性,高度不能超过父窗体,超过父窗体的阴影部分会被截取掉,并且设置阴影高度的控件背景不能是透明的 android:elevation="30dp" 使用
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:elevation="20dp" android:background="#FFFFFF" android:text="阴影" android:outlineProvider="background" /> </RelativeLayout>
3.2 通过设置elevation属性实现控件重叠效果 将图片存放到mipmap-hdpi(用来替换drawable的,它在屏幕处理的时候效率会更高)目录中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:elevation="20dp" android:background="#FFFFFF" android:text="阴影" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/head" android:background="#FFFFFF" android:elevation="10dp" android:layout_margin="10dp" /> </RelativeLayout>
3.3 TranslationZ设置 elevation是设置阴影,TranslationZ是z轴上移动的距离(可以通过TranslationX属性进行验证),因为z轴移动所以动态带有阴影
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:translationZ="20dp" android:background="#FFFFFF" android:text="阴影" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/head" android:background="#FFFFFF" android:translationZ="25dp" android:layout_margin="10dp" /> </RelativeLayout>
-
轮廓形状的显示方式 发现TextView的阴影的形状是方形的,如何改成圆形,改变TextView的背景即可 <TextView android:id="@+id/textView" android:layoutwidth="wrapcontent" android:layoutheight="wrapcontent" android:translationZ="20dp" android:background="@drawable/circleshape" android:text="阴影" android:layoutmargin="10dp" android:layoutcenterInParent="true" /> ImageView也一样 <ImageView android:layoutwidth="wrapcontent" android:layoutheight="wrapcontent" android:src="@mipmap/head" android:background="@drawable/circleshape" android:translationZ="65dp" android:layoutmargin="10dp" android:layoutcenterInParent="true" /> 不想去除背景再把阴影改为方形,需要使用outlineProvider属性 <ImageView android:layoutwidth="wrapcontent" android:layoutheight="wrapcontent" android:src="@mipmap/head" android:background="@drawable/circleshape" android:outlineProvider="bounds" android:translationZ="25dp" android:layoutmargin="10dp" android:layout_centerInParent="true" /> outlineProvider属性含义 background : 以背景显示阴影 bounds : 以控件的边框显示阴影 paddedBounds : 有padding值的时候阴影会往里收
代码设置 根据文本代码设置轮廓找到相应的xml文件,根据xml文件中的id找到相应的java类中的操作 view.setOutlineProvider(new ViewOutlineProvider(){ public void getOutLine(view,outine){ //开始位置,结束位置,宽,高 outline.setOval(0,0,view.getWidth(),view.getHeight()); } }); 注意:阴影不会超出父view范围 4.图片剪裁 根据文本剪裁成圆形找到相应的xml文件,根据xml文件中的id找到相应的java类中的操作 布局文件 <TextView android:id="@+id/cut" android:layoutwidth="50dp" android:layoutheight="50dp" android:background="#FF0000" android:text="剪裁成圆形" /> 代码操作 //剪裁图片 TextView mCut = (TextView) findViewById(R.id.cut); //设置轮廓的剪裁操作 mCut.setOutlineProvider(new ViewOutlineProvider() { @Override public void getOutline(View view, Outline outline) { outline.setOval(0,0,view.getWidth(),view.getHeight()); } }); mCut.setClipToOutline(true);//使轮廓剪裁生效
-
图片选择器 tint属性 根据tint选择器找到相应的xml文件,发现是其实是状态选择器,不过其中使用bimap标签进行操作 drawable目录创建selector <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:statepressed="true" android:drawable="@drawable/tintbitmap"> <item android:drawable="@mipmap/ring"> 在drawable目录下创建bitmap标签操作 <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@mipmap/ring" android:tintMode="multiply" android:tint="#FF0000" > <!-- tintMode : 类型 tint : 颜色 --> 使用 android:background="@drawable/tint_selector"
-
图片颜色抽取 Palette 根据文本Vibrant找到相应的xml文件,发现就是一个textview,查看Java文件中操作
ImageView mColorIcon = (ImageView) findViewById(R.id.coloricon); final TextView mColor = (TextView) findViewById(R.id.color); BitmapDrawable drawable = (BitmapDrawable) mColorIcon.getBackground(); //使用调用色板 Palette.generateAsync(drawable.getBitmap(),new Palette.PaletteAsyncListener(){ @Override public void onGenerated(Palette palette) { //调色板上最鲜艳的色彩,参数:默认颜色 mColor.setBackgroundColor(palette.getVibrantColor(Color.BLACK)); } });
-
按压时的波纹效果 Android5.0按钮自带的效果 还提供两种额外的效果 ?android:attr/selectableItemBackground : 方形渐变的效果 android:attr/selectableItemBackgroundBorderless : 圆形渐变效果
-
自定义波纹动画 根据圆形缩小动画找到相应的xml文件,根据控件的id,查看java文件中如何实现
//缩小动画 final Button bt1 = (Button) findViewById(R.id.bt1); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //参数1:执行动画控件 //参数2,参数3:动画的中心点 //参数4,参数5:开始的角度和结束的角度 Animator animator1 = ViewAnimationUtils.createCircularReveal(bt1, bt1.getWidth() / 2, bt1.getHeight() / 2, bt1.getWidth(), 0); animator1.setInterpolator(new LinearInterpolator());//设置动画插补器 animator1.setDuration(1000); animator1.start(); } });
-
状态动画 根据状态动画找到相应的xml文件,发现其中使用的stateListAnimator属性,查看属性的值,发现是一个selector状态选择器 拷贝android5.0Demo中的stateanim,发现其就是状态选择器,包含了很多属性动画 给按钮设置stateListAnimator使用stateanim <Button android:layout_width="100dp" android:layoutheight="100dp" android:stateListAnimator="@drawable/stateanim" />
-
RecycleView 10.0 添加 compile 'com.android.support:recyclerview-v7:23+' 10.1 展示效果:可以垂直/水平显示的列表/瀑布流,功能强大,用于替代ListView 10.2 增加到布局文件 10.3 find到view对象,并设置Adapter 10.4 设置布局管理器
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activityreclyeview); RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(layoutManager); mRecyclerView.setAdapter(new Myadapter()); } private class Myadapter extends RecyclerView.Adapter<Myadapter.ListHoldewer>{ //设置条目 @Override public ListHoldewer onCreateViewHolder(ViewGroup parent, int viewType) { TextView textview = new TextView(ReclyeViewActivity.this); return new ListHoldewer(textview); } //获取条目数据 @Override public void onBindViewHolder(ListHoldewer holder, int position) { holder.setData(position); } //获取条目个数 @Override public int getItemCount() { return 100; } class ListHoldewer extends RecyclerView.ViewHolder{ TextView textview; public ListHoldewer(View itemView) { super(itemView); textview = (TextView) itemView; } public void setData(int position) { textview.setText("当前的位置:"+position); } } }
-
CardView 实现卡片式的效果 11.0 添加com.android.support:cardview-v7:23+ 布局文件操作 <android.support.v7.widget.CardView xmlns:cardview="http://schemas.android.com/apk/res-auto" android:layoutwidth="match_parent" android:layoutheight="matchparent" > </android.support.v7.widget.CardView>
-
SwipeRefreshLayout 12.0 展示效果:下拉刷新列表 12.1 增加到布局文件 <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/refresh" android:layoutwidth="matchparent" android:layoutheight="matchparent">
<android.support.v7.widget.RecyclerView android:id="@+id/recycler" android:padding="5dp" android:background="#eaeaea" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.v4.widget.SwipeRefreshLayout>
12.2 find到view对象 refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh); 12.3 设置下拉监听 refreshLayout.setOnRefreshListener(this); @Override public void onRefresh() { refreshLayout.postDelayed(new Runnable() { @Override public void run() { refreshLayout.setRefreshing(false); } },3000); } 12.5 更改颜色
refreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
13.ToolBar 13.1 用于替代ActionBar,继承自ViewGroup可以任意包裹子布局,灵活性更高 13.2 使用时必须设置背景色 <Toolbar android:id="@+id/toolbar" android:layoutwidth="matchparent" android:layout_height="150dp" android:background="#FF0000"/> 13.3 设置主题,隐藏ActionBar parent="Theme.AppCompat.NoActionBar" requestWindowFeature(Window.FEATURENOTITLE); 13.4 主界面继承自AactionBarActivity,onCreate方法执行时设置ToolBar作为ActionBar toolbar = (Toolbar) findViewById(R.id.toolbar); setActionBar(toolbar);
-
okHttp 14.1 okHttp用于替代HttpUrlConnection和Apache HttpClient(6.0里已移除HttpClient) 14.2 okHttp优势 1.2.1 支持SPDY(Google开发的基于TCP的应用层协议,用以最小化网络延迟,提升网络速度,优化用户的网络使用体验),共享同一个Socket来处理同一个服务器的所有请求 1.2.2 如果SPDY不可用,则通过连接池来减少请求延迟 1.2.3 无缝的支持GZIP来减少数据流量 1.2.4 缓存响应数据来减少重复的网络请求 14.3 导入依赖 file - project structrue - app - dependebcies - library dependencies - 搜索okhttp 14.4 发送get请求 1.3.1 创建OkHttpClient对象 1.3.2 请求 请求链接地址:http://wthrcdn.etouch.cn/weathermini?citykey=101010100 public void get(){ new Thread(){ @Override public void run() { //获取okhttp客户端对象 OkHttpClient okHttpClient = new OkHttpClient(); //生成请求 Request request = new Request.Builder().url("http://wthrcdn.etouch.cn/weathermini?citykey=101010100").build(); //创建请求操作 Call call = okHttpClient.newCall(request); try { //请求 Response response = call.execute(); //判断是否链接成功 if (response.isSuccessful()){ //获取请求数据 String msg = response.body().string(); Log.e("MainActivity", "MainActivity,run: "+msg);
} } catch (IOException e) { e.printStackTrace(); } super.run(); } }.start(); }
14.5 发送post请求,并传递字段 //post请求 RequestBody requestBody = new FormEncodingBuilder().add("name","ls").add("psw","123").build(); //生成请求 Request request = new Request.Builder().url("http://wthrcdn.etouch.cn/weather_mini?citykey=101010100").post(requestBody).build();
14.6 发送json数据 //设置请求链接 MediaType mediaType = MediaType.parse("application/json;charset=utf-8"); //设置传递的json数据 String json = "{name:\"张三\"}"; //设置json请求 RequestBody requestBody = RequestBody.create(mediaType,json); //生成请求 Request request = new Request.Builder().url("http://wthrcdn.etouch.cn/weather_mini?citykey=101010100").post(requestBody).build();
-
TextInputLayout 主题不能使用android:Theme.Material.Light主题使用默认的主题 15.1.环境配置 添加compile 'com.android.support:design:23+' 15.2.布局文件 <android.support.design.widget.TextInputLayout android:id="@+id/textinput" android:layoutwidth="matchparent" android:layoutheight="wrapcontent"> <EditText android:layoutwidth="matchparent" android:layoutheight="wrapcontent" android:hint="账号"/> </android.support.design.widget.TextInputLayout> 15.3.代码操作 final TextInputLayout textinput = (TextInputLayout) findViewById(R.id.textinput); //获取输入操作 EditText editText = textinput.getEditText(); //设置输入监听 editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { //判断输入的文字不超过十个字 if (s.length() > 10){ //开启错误提醒 textinput.setErrorEnabled(true); //进行错误提醒 textinput.setError("请不要超过十个字"); }else{ //关闭错误提醒 textinput.setErrorEnabled(false); } } });
-
FloatingActionButton(FAB) 16.1.环境配置 compile 'com.android.support:design:23+' 16.2.布局文件<android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layoutwidth="wrapcontent" android:layoutheight="wrapcontent" android:layoutalignParentBottom="true" android:layoutalignParentRight="true" android:src="@mipmap/icdone" android:layoutmargin="20dp" app:pressedTranslationZ="90dp" app:rippleColor="#00FF00" app:fabSize="normal" app:backgroundTint="#0000FF" />
16.3.点击事件的实现
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"我被点击了",Toast.LENGTH_LONG).show();; } });