Android Tips
2016-03-05 05:27
513 查看
Toast in fragment
When we want to make toast in fragments, we should usegetActivity() to get application context.
Toast.makeText(getActivity(), "Here is a toast", Toast.LENGTH_LONG).show();
findViewById in fragment
When we want to find view in onCreateView() in a fragment, we should first inflate the layout,View view = inflater.inflate(R.layout.fragment_2, parent, false); //This view should be returned at the end
then use view to find the view that we want.
(Button) view.findViewById(R.layout.btn);
Navigation drawer 1
While using navigation drawer, we can implements actions in selectItem(int position). There is no need to delete the preset code (which is indicated below). These codes are used for close drawer, etc. Unless you want to do something else.private void selectItem(int position) { mCurrentSelectedPosition = position; if (mDrawerListView != null) { mDrawerListView.setItemChecked(position, true); } if (mDrawerLayout != null) { mDrawerLayout.closeDrawer(mFragmentContainerView); } if (mCallbacks != null) { mCallbacks.onNavigationDrawerItemSelected(position); }
Just simply use a switch to get the position that user selected.
private void selectItem(int position) { //Preset codes switch (position) { case 0: //Things to do. break; case 1: //Things to do. break; default: break; }
ScrollView fill page
Just simply use the XML code follow<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true" android:fadeScrollbars="false" android:scrollbars="vertical" >
Create Activity
In function onCreate of an Activity, we should@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.nameOfLayout); setTitle(R.string.nameOfLayout); }
And,
PersistableBundle persistentState
shall not be one of the argument of function onCreate.
Moreover,
super.onCreate(savedInstanceState);
shall be written before setContentView, otherwise, the layout may not be load into the Activity.
相关文章推荐
- Three ways to implements an Android OnClickListener
- Android AsyncTask基础
- 01Android学习从零单排之Activity
- Android View中getWidth方法
- android设计模式之---状态模式
- Android开发60需要注意的问题
- Android下设置drawableleft导入的图片大小
- Android Studio中如何创建自己的库?
- 2016-3-4
- Gradle
- android 注解 butterknife 用法
- Android开发中iconify的使用
- Android优化杂谈
- Android ListView滚动条配置完全解析
- android设计模式之---最佳单例模式==静态内部类单例模式
- Android Studio——签名打包
- Android 使用Fragment界面向下跳转并一级级返回
- Android Studio 中Locat使用包名过滤无法显示的问题
- Android性能优化——渲染、内存、电源优化
- System Permissions --1.4