Android 学习月报 2016-5月
注意使用 databing 导包问题:
com.google.common.reflect.TypeToken Databing
com.google.gson.reflect.TypeToken Gson
Databing 使用的坑
修改ViewModel 的名字,
在xml 中定义 drawable 图片数组
- 在xml中要这样定义,而不是用
integer-int <string-array name="actions_images">
<item>@drawable/pencil1</item>
<item>@drawable/pencil2</item>
<item>@drawable/pencil3</item>
</string-array>
- 在代码中这样来获取:
TypedArray ar = context.getResources().obtainTypedArray(R.array.actions_images);
int len = ar.length();
int[] resIds = new int[len];
for (int i = 0; i < len; i++)
resIds[i] = ar.getResourceId(i, 0);
ar.recycle();
- 引用 img.setImageResourse(resIds[0])来设置图片
在Fragment 中设置menu ,要在onCreate() 方法中加入 setHasOptionsMenu(true) ,否则不显示
#### 设置 CollapsingToolbarLayout title 居中
CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setExpandedTitleGravity(Gravity.CENTER_HORIZONTAL);
collapsingToolbar.setCollapsedTitleGravity(Gravity.CENTER_HORIZONTAL);
设置 EditText 光标的颜色
android:textCursorDrawable="@null" //让光标颜色和text color一样
Android BottomSheet 默认展开所有
BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(this);
View contentView = LayoutInflater.from(this).inflate(R.layout.view_bottomsheet_pay, null);
mBottomSheetDialog.setContentView(contentView);
BottomSheetBehavior mDialogBehavior = BottomSheetBehavior.from((View) contentView.getParent());
mDialogBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
开发中在ListView中经常有Button或ImageButton等需要被点击的控件,如果不加一些特殊的限制,有可能ListView的Item的点击事件或Button的点击事件,其中一个不能响应。我遇到的情况是ListView的Item不能响应点击事件。
解决的办法,在ListView的Item的xml文件中添加如下属性:
1.整个xml文件的根元素如LinearLayout中添加属性android:descendantFocusability="blocksDescendants"
- Button中添加属性android:focusable="false"和android:clickable="true"
黑色的 statusbar icon
绘制渐变背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FF000000"
android:centerColor="#FF000000"
android:endColor="#FF777777"
android:angle="270"
/>
</shape>
startColor起始颜色,endColor结束颜色,angle表示方向角度。当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上,270从上到下。
Fragment 中添加 Menu 菜单,必须 在 onCreate 中设置sethastOptionMenu。否则不显示 Menu,但界面切换时导致
Menu 莫名其妙消失,解决办法 要在onCreateOpenMenu 调用父类方法时 inflate menu view。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_fragment_home, menu);
super.onCreateOptionsMenu(menu, inflater);
}
layout不能设置 color selecter,要设置为 drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/tab_text_normal" android:state_selected="false"/>
<item android:color="@color/tab_text_selected" android:state_selected="true"/>
</selector>
Android 推送原理
1.一种是定时去server查询数据,通常是使用HTTP协议来访问web服务器,称Polling(轮询);
2.还有一种是移动端和服务器建立长连接,使用XMPP长连接,称Push(推送)。
while(true) {
request(timeout);
request(timeout);
}
客户端发出一个“长”请求,如果服务器发送消息或者时间out了,客户端就断开这个请求,再建立一个长请求从耗费的电量、流量和数据延迟性各方面来说,Push有明显的优势。但是使用Push的缺点是:对于客户端:实现和维护相对成本高,在移动无线网络下维护长连接,相对有一些技术上的开发难度。
WebView in ScrollView
ScrollView和WebView都有滚动的效果,所以我们需要先屏蔽WebView的滚动事件。
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants" >
Auto Scale
这时候如果后端如果传过来的不是完整的Html,而是只有body部分的内容,那么我们就需要补充并添加一些css样式来达到自适应的效果。
WebView webView = new WebView(this);
webView.setWebViewClient(new SimpleWebViewClient(title));
webView.getSettings().setDefaultTextEncodingName("utf-8");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
} else {
webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
}
webView.loadData(getHtmlData(body), "text/html; charset=utf-8", "utf-8");
拼接 HTML
private String getHtmlData(String bodyHTML) {
String head = "<head>" +
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " +
"<style>img{max-width: 100%; width:auto; height:auto;}</style>" +
"</head>";
return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
}
WebView 乱码
使用 loadData方法是中文部分会出现乱码,即使指定“utf-8”、“gbk”、“gb2312”也一样。
webView.getSettings().setDefaultTextEncodingName("UTF -8");//设置默认为utf-8
// webView.loadData(data, "text/html", "UTF -8");//API提供的标准用法,无法解决乱码问题
webView.loadData(data, "text/html; charset=UTF-8", null);//这种写法可以正确解码
或者考虑使用 loadDataWithBaseURL方法
用 Shape 实现下划线的效果
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:left="-16dp"
android:right="-16dp"
android:top="-16dp">
<shape>
<solid android:color="#00FFFFFF" />
<stroke
android:width="2px"
android:color="@color/colorPrimary" />
</shape>
</item>
</layer-list>
Use Android Data Binding with include tag
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<import type="com.liangfeizc.databindingsamples.basic.User" />
<variable
name="user"
type="User" />
<variable
name="contact"
type="com.liangfeizc.databindingsamples.custombindings.Contact" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/user"
bind:user="@{user}" />
<include
layout="@layout/contact"
bind:contact="@{contact}" />
</LinearLayout>
</layout>
统一思想判断是否是 Array或者 Object
android 中 使用 JSONObject 或者 JSONArray 强制try catch 来捕获错误
dp与px换算公式:
pixs =dips * (densityDpi/160).
dips=(pixs*160)/densityDpi