1.使用fragment,主类要定义为public,否则会出现java.lang.IllegaAccessException:access to class not allowed异常
解决方案地址:http://blog.csdn.net/lonelyroamer/article/details/7907501
2.RelativeLayout里面定义的EidtText设置宽度为match_parent高度为固定的,可以自适应。
设置layout:margin的上线左右间距
padding设置字符与上线左右的间距
解决光标不显示的问题:android:textCursorDrawable="@null"
android:textColor设置字符的颜色
android:hint=""设置默认显示的字符
3.RelativeLayout中使用android:backgroud=""设置布局文件的背景颜色
4.Fragment之间传递数据方法:
http://blog.csdn.net/zwluoyuxi/article/details/37600625 感谢分享~
5.监听键盘按键
@Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { Log.e("everydayword","actionid is ============= "+actionId); Log.e("everydayword","event is ============= "+event); if(event.getAction() == KeyEvent.ACTION_DOWN ){ /*隐藏软键盘*/ InputMethodManager imm = (InputMethodManager) v .getContext().getSystemService( Context.INPUT_METHOD_SERVICE); downJson(); return true; } return false; }
之前使用actionid判断会出现调用两次的问题,因为keyEvent有actiondown和actionup两个操作的actionid都是0所以会调用两次,所以应该用keyevent的值来判断
inputSearch.setOnEditorActionListener(this);
6.如果没有输入内容或者输入的是空格则弹出toast
//去掉空格判断是否是空
if(input.replace(" ","").equals("")){
toast();
}
public void toast(){
Toast toast = null;
if(toast == null){
toast = Toast.makeText(getActivity().getApplicationContext(),"o(╯□╰)o 你还没有输入单词哦~",Toast.LENGTH_SHORT);
} else {
toast.setText("o(╯□╰)o 你还没有输入单词哦~");
}
toast.show();
}
7.使用HorizontalScrollView实现侧边栏滑动
a.修改启动layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<com.everydayword.sunyingying.everydayword.MenuLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/sliding"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<include layout="@layout/menulayout"/>
<include layout="@layout/activity_main"/>
</LinearLayout>
</com.everydayword.sunyingying.everydayword.MenuLayout>
</FrameLayout>
HorizontalScrollView必须在FrameLayout中实现,menulayout是侧边栏的layout,activity_main是右侧(原来的启动layout)界面
滑动效果类
public class MenuLayout extends HorizontalScrollView {
private int padding = 200;
private int screenWidth;
private boolean isChanged;
private ViewGroup leftMenu;
private ViewGroup rightContent;
private int menuWidth;
public MenuLayout(Context context) {
this(context,null,0);
}
public MenuLayout(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public MenuLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
//Use Context.getSystemService(Context.WINDOW_SERVICE) to get one of these.(from android developer)
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
//To access the DisplayMetrics members, initialize an object like this: (from android developer)
DisplayMetrics outMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(outMetrics);
screenWidth = outMetrics.widthPixels;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (!isChanged) {
LinearLayout wrapper = (LinearLayout) getChildAt(0);
leftMenu = (ViewGroup) wrapper.getChildAt(0);
rightContent = (ViewGroup) wrapper.getChildAt(1);
menuWidth = screenWidth - padding;
leftMenu.getLayoutParams().width = menuWidth;
rightContent.getLayoutParams().width = screenWidth;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (changed) {
this.scrollTo(menuWidth, 0);
isChanged = true;
}
}
}
8.fragment和activity通信
frgment中创建接口,activity实现接口,回调函数
fragment中代码:
public interface ExplainsCallback{ public void openDrawer(); }
@Override public void onClick(View v) { switch (v.getId()){ case R.id.clear: clearContent(); break; case R.id.menu: if(getActivity() instanceof ExplainsCallback){ ((ExplainsCallback)getActivity()).openDrawer(); } break; } }activity中代码:
/** * 实现ExplainsCallback的方法,点击按钮弹出侧边栏 */ @Override public void openDrawer() { Log.e("everydayword","mainactivity call back"); drawerLayout.openDrawer(Gravity.LEFT); }
9.发送api请求转码
String searchStr = "http://fanyi.youdao.com/openapi.do?keyfrom=EverydayWord&key=1955279509&type=data&doctype=json&version=1.1&q="+ URLEncoder.encode(input, "UTF-8");10.自定义颜色值
values文件夹下创建color.xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="explainbackground">#1cb7dc</color> </resources>使用:
int colorBackground = getResources().getColor(R.color.explainbackground);
colorBackground就是得到的颜色值
10.listview设置分割线的颜色和宽度
android:divider="#e8e8e8" android:dividerHeight="1dp"