关于教务系统项目所遇到的一些知识点总结
1.Activity跳转Activity
方法一
Intent intent = new Intent(当前Activity.this, 目标Activity.class);
putExtra("标志",数据);//当需要在跳转时传递数据用这个语句
startActivity(intent);
2.Activity跳转Fragment
方法一
Intent intent = new Intent(Activity.this ,Fragment所在Activity.class);
putExtra("标志",int); // 用来标记要跳转的是哪个fragment
startActivity(intent);
方法二
Intent intent = new Intent(Activity.this ,Fragment所在Activity.class);
startActivity(intent)
在Fragment所在Activity里边处理切换fragment的逻辑
3.Fragment跳转Activity
方法一
Intent intent = new Intent(getActivity(), 目标Activity.class);
startActivity(intent);
4.代码生成View
LinearLayout
LinearLayout lly = new LinearLayout(getContext()); // 获取上下文context
lly.setOrientation(LinearLayout.VERTICAL); //方向竖直
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT ,firstHeight); //设置宽和高
// 可以通过这个addRule添加规则,第一个参数为方位,第二个参数为相对于哪个view的id
params.addRule(RelativeLayout.RIGHT_OF ,left_top_Text.getId());
lly.setLayoutParams(params);
ScrollView
注意:ScrollView只能有一个子view(当然你可以把你想要写的多个view用Layout包起来)
ScrollView scrollView = new ScrollView(getContext());
scrollView.setId(R.id.scrollView);
LayoutParams rlp_sv = new LayoutParams(LayoutParams.MATCH_PARENT , LayoutParams.WRAP_CONTENT);
//如果你需要设置它相对于某个控件的位置就可以使用下面这条语句和上面Layout的用法一样
rlp_sv.addRule(RelativeLayout.BELOW ,left_top_Text.getId());
scrollView.setLayoutParams();
FramLayout
FraeLayout frameLayout = new FrameLayout(getContext());
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT );
frameLayout.setLayoutParams(layoutParams);
TextView
TextView tv = new TextView(getContext());
tv.setHeight(height);
tv.setWidth(width);
tv.setTextColor(getResources().getColor(R.color.text1_color)); //这里引用了一个values中colors.xml里的自定义颜色
tv.setTextSize(14);
tv1.setGravity(CENTER_HORIZONTAL); //在LinearLayout的VERTICAL条件下
tv.setText("内容") ;
ProgressDialog
这里直接给出了ProgressDialog的封装好的开启和关闭的方法!
//开启
private void showPressDialog(){
if(progressDialog == null){
progressDialog = new ProgressDialog(this);
progressDialog .setMessage("正在加载...");
progressDialog.setCanceledOnTouchOutside(false);
}
progressDialog.show();
}
//关闭
private void closeProgressDialog(){
if(progressDialog != null){
progressDialog.dismiss();
}
}
4.Fragment切换Fragment
//import.support.v4.app.FragmentTransaction;
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction;
transaction.replace(R.id.frame_content, new CenterFragment());
transaction.commit();
5.指定x,y坐标生成view
LayoutParams params = new LayoutParams(x,y);//x,y分别为距离左边屏幕和上边屏幕的距离
控件对象.setLayoutParams(params);
6.自定义背景Drawable
这里只给出我用到的一些语法
<?xml version="1.0" encoding="utf-8">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="0dp"> // 一个item相当于一个独立的drawable
<shape android:shape="rectangle"> //shape表示形状
<solid android:color = "#344AF7"/> //solid表示纯色填充,通过android:color设置颜色
<corners android:topRightRadius ="10dp" //为右上角设定角度(圆角)
android:topLeftRadius ="10dp"/> //为左上角设定角度(圆角)
</shape>
</item>
<item android:bottom = "0.5dp" //与上边的距离
android:right="0.5dp" //与右边的距离
android:left="0.5dp" > //与左边的距离
<shape android:shape="rectangle">
<corners android:topRightRadius ="10dp"
android:topLeftRadius ="10dp"/>
<solid android:color ="#cbeff6"/>
</shape>
</item>
7.viewPager+tablayout
这个之前博客有这里给链接:[viewPager+tabLayout的代码]
viewPager+tabLayout的讲解(https://blog.csdn.net/sliverbullets/article/details/79437951)