基础框架搭建(一)
来源:互联网 发布:qq阅读网络异常怎么办 编辑:程序博客网 时间:2024/05/30 12:30
启动页面
1.启动页面代码:
public class SplashActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_splash); //两秒后延迟进入主界面 new Handler().postDelayed(new Runnable() { @Override public void run() { startActivity(new Intent(SplashActivity.this,MainActivity.class)); finish(); } },2000); }}2_启动页面布局文件
<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"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical"> <ImageView android:id="@+id/logo" android:layout_width="72dp" android:layout_height="72dp" android:layout_centerInParent="true" android:layout_gravity="center" android:focusable="true" android:src="@drawable/logo" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:text="欢迎你" android:textColor="#eb2b84" android:textSize="18sp" /> </LinearLayout></RelativeLayout>主页面
1_主页面布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <RadioGroup android:id="@+id/rg_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/home_bottom_parent_bg" android:orientation="horizontal"> <RadioButton android:id="@+id/rb_home" style="@style/MainButtonStyle" android:drawableTop="@drawable/home_button_selector" android:text="首页" /> <RadioButton android:id="@+id/rb_type" style="@style/MainButtonStyle" android:drawableTop="@drawable/type_button_selector" android:text="分类" /> <RadioButton android:id="@+id/rb_community" style="@style/MainButtonStyle" android:drawableTop="@drawable/community_button_selector" android:paddingTop="10dp" android:text="发现" /> <RadioButton android:id="@+id/rb_cart" style="@style/MainButtonStyle" android:drawableTop="@drawable/cart_button_selector" android:text="购物车" /> <RadioButton android:id="@+id/rb_user" style="@style/MainButtonStyle" android:drawableTop="@drawable/user_button_selector" android:text="个人中心" /> </RadioGroup></LinearLayout>02_按钮样式
<style name="MainButtonStyle"> <item name="android:layout_width">0dp</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_weight">1</item> <item name="android:textColor">@drawable/bottom_button_text_selector</item> <item name="android:button">@null</item> <item name="android:gravity">center</item></style>03_设置按钮选择器
2_使用 Butterknife 初始化布局
01_配置快捷键
1)在 Setting->Plugins 中输入 butterknife 添加插件
2)Android ButterKnife Aelezny ->点击安装
02_Module 里的 build.gradle 里面添加
compile 'com.jakewharton:butterknife:8.0.1'
03_在 MainActivity 中使用
@Bind(R.id.frameLayout)FrameLayout frameLayout;@Bind(R.id.rb_home)RadioButton rbHome;@Bind(R.id.rb_type)RadioButton rbType;@Bind(R.id.rb_community)RadioButton rbCommunity;@Bind(R.id.rb_cart)RadioButton rbCart;@Bind(R.id.rb_user)RadioButton rbUser;@Bind(R.id.rg_main)RadioGroup rgMain;2_Fragment 的基类和各个子 Fragment
public abstract class BaseFragment extends Fragment{ public Context mContext; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext=getActivity(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return initView(); } public abstract View initView(); @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { initData(); super.onViewCreated(view, savedInstanceState); } public void initData(){ }}02_各个子 Fragment 创建
首页:HomeFragment
分类:TypeFragment
发现:CommunityFragment
购物车:ShoppingCartFragment
用户中心:UserFragemnt
其中某个页面的内容如下:
public class UserFragment extends BaseFragment { private static final String TAG = UserFragment.class.getSimpleName(); private TextView textView; @Override public View initView() { Log.e(TAG,"用户中心视图被初始化了"); textView = new TextView(mContext); textView.setGravity(Gravity.CENTER); textView.setTextSize(25); textView.setTextColor(Color.RED); return textView; } @Override public void initData() { super.initData(); Log.e(TAG,"用户中心数据被初始化了"); textView.setText("用户中心"); }}3_初始化各个 Fragment 并且切换
01_初始化各个 Fragment
记得在 onCreate 地方调用
private void initFrament() { fragments=new ArrayList<>(); fragments.add(new HomeFragment()); fragments.add(new TypeFragment()); fragments.add(new CommunityFragment()); fragments.add(new ShoppingCartFragment()); fragments.add(new UserFragment());}
@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); initFrament(); initListener();}02_各个 Fragment 的切换
private void initListener() { rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { switch (checkedId){ case R.id.rb_home: position=0; break; case R.id.rb_type: position=1; break; case R.id.rb_community: position=2; break; case R.id.rb_cart: position=3; break; case R.id.rb_user: position=4; break; } BaseFragment baseFragment = getFragment(position); switchFragment(tempFragemnt, baseFragment); } }); rgMain.check(R.id.rb_home);}根据位置得到对应的 Fragment
private BaseFragment getFragment(int position) { if(fragments!=null&&fragments.size()>0){ BaseFragment baseFragment = fragments.get(position); return baseFragment; } return null;}切换fragment
private void switchFragment(BaseFragment fromFragment, BaseFragment nextFragment) { if(tempFragemnt!=nextFragment){ tempFragemnt=nextFragment; if(nextFragment!=null){ FragmentTransaction ft=getSupportFragmentManager().beginTransaction(); if(!nextFragment.isAdded()){ if(fromFragment!=null){ ft.hide(fromFragment); } ft.add(R.id.frameLayout,nextFragment).commit(); }else{ if(fromFragment!=null){ ft.hide(fromFragment); } ft.show(nextFragment).commit(); } } }}
阅读全文
0 0
- 基础框架搭建(一)
- ssm+shiro基础框架搭建(一)
- spring boot 搭建【搭建基础框架】(一)
- sea.js最佳实践一基础框架搭建
- Spring4学习(一):eclipse插件安装,基础框架搭建
- grails框架入门小结(一)—后台基础搭建
- 迭代(一)搭建框架基础体系
- spring boot 1.5.7 搭建基础项目框架一
- SpringMVC基础框架搭建
- SSH-基础框架搭建
- struts2框架 基础搭建
- RBAC基础框架搭建
- ThinkPHP框架一 框架搭建
- 框架搭建一
- 三大框架---hibernate框架基础笔记(个人理解,环境搭建及api)--一
- SS2H框架搭建,基础上篇
- SSH笔记---基础框架搭建
- 从零开始搭建VUE基础框架
- Codeforces Round #446 (Div. 2) A-C 题解
- BZOJ 刷题计划
- 比特币源码linux下环境配置编译运行bitcoin
- freemarker导出word模板
- 安迪-比尔定理 (Andy and Bill’s Law)还有效吗?
- 基础框架搭建(一)
- 【AI英雄风云榜】为TA投票:谁是2017中国AI领域最牛的人?
- the thinking use of DOM in my project
- Hello World!
- MySql --MVCC实现原理(乐观锁实现原理)
- 数据结构AOE网
- 爬虫LOL王者信息----Jupyter Notebook
- 使用Construct 2制作HTML5游戏——完善
- 理解 Java 序列化