1.工具:Android Studio
2.Android Studio 设置添加一个类头文件
File-Settings-Editor-File and Code Templates-File Header
设置这个的作用是为每一个新生成的类添加一个注释,效果如下!
3.为项目添加对应的包
(1)View 存放视图界面
(2)Utils 工具类
(3)ui 存放ui,界面交互的
(4)entity 存放实体类
(5)adapter 存放适配器文件
(6)appliction 存放统一的初始化操作
(7)fragment 存放Fragment文件
(8)service 类似于后台的文件处理
<!----------------------------------------------------------------------------------------------------------------------------->
(1)Values 主要是做语言的适配,例如中文Values-zh-rCN中国 values-zh-rUS美国
3.目录啥的都折腾好了!接下来就是创建类了
在Appliction中创建一个类(BaseApplication)继承 Application,用来实现初始化操作,目前还用不到所以先创建一个空白类继承即可!
package com.example.qqazl001.smartbutler.application;
import android.app.Application;
/*
* 项目名:SmartButler
* 包名: com.example.qqazl001.smartbutler.application
* 文件名:BaseAppliction
* 创建者: RUI
* 创建时间:2018-08-12 15:29
* 描述: A
*/public class BaseAppliction extends Application{
@Override
public void onCreate() {
super.onCreate();
}
}
然后去清单文件(AndroidManifest.xml)把默认的Appliction替换成我们新建的Appliction,在applicaton中添加name属性
android:name=".application.BaseAppliction"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.qqazl001.smartbutler">
<application
android:name=".application.BaseAppliction"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:theme="@style/NoActivityFullcren">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</manifest>
4.创建一个Activity的基类,BaseActivity继承AppCompatActivity
主要做的事情如下:(此处暂时统一,显示返回键,增加菜单栏操作)
(1) 统一的属性
(2) 统一的方法
(3) 统一的接口
package com.example.qqazl001.smartbutler.ui;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
/*
* 项目名:SmartButler
* 包名: com.example.qqazl001.smartbutler.ui
* 文件名:BaseActivity
* 创建者: RUI
* 创建时间:2018-08-15 21:19
* 描述:Activity基类
*/
public class BaseActivity extends AppCompatActivity{
/*
* 主要做的事情
* 1.统一的属性
* 2.统一的方法
* 3.统一的接口
* */
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//显示返回键
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
//菜单栏操作
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}
5.Drawable 存放资源文件,官方的mipmap文件夹只是用来放启动图标的,AndroidStudio新建项目的ic_launcher.png都是默 认放在mipmap文件夹下的。
增加几个Drawable目录,根据不同分辨率适配!
自此准备工作都做完了!
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6.使用TabLayout配合ViewPage,实现以下界面
在activity_main.xml文件中添加一个TabLayout,和一个ViewPage,还有一个设置的悬浮按钮!要使用TabLayout需要声明这个头文件
xmlns:app="http://schemas.android.com/apk/res-auto"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/mTableLayout"
android:background="@color/colorPrimary"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorAccent"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorAccent"
app:tabTextColor="@android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<android.support.v4.view.ViewPager
android:id="@+id/mViewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
<!--悬浮的设置按钮-->
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_setting"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
添加一个设置按钮跳转设置的Activity,Setting_Activity
<?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">
<TextView
android:text="@string/setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Setting_Activity需要继承我们前面编写的BaseActivity类,因为这个类默认就添加好了返回的功能
package com.example.qqazl001.smartbutler.ui;
import android.os.Bundle;
import android.support.annotation.Nullable;
import com.example.qqazl001.smartbutler.R;
/*
* 项目名:SmartButler
* 包名: com.example.qqazl001.smartbutler.ui
* 文件名:Setting_Activity
* 创建者: RUI
* 创建时间:2018-08-15 23:56
* 描述: 设置*/
public class Setting_Activity extends BaseActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
}
}
7.接下来创建4个布局文件,暂时内容就写着跟标题一样的fragment_butler、fragment_girl、fragment_girl、fragment_wechat,四个布局都一样只是内容文本不同而已,所以只放一个代码就好了
<?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:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="智能管家"/>
</LinearLayout>
8.在fragment文件夹创建四个类继承Fragment并且加载上面的四个布局文件ButleFragment、GirlFragment、UserFragment、WechatFragment,四个布局都一样只是加载布局不同而已,所以只放一个代码就好了
package com.example.qqazl001.smartbutler.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.qqazl001.smartbutler.R;
import static com.example.qqazl001.smartbutler.R.layout.fragment_butler;
/*
* 项目名:SmartButler
* 包名: com.example.qqazl001.smartbutler.fragment
* 文件名:ButlerFragment
* 创建者: RUI
* 创建时间:2018-08-14 21:53
* 描述: 管家服务
*/public class ButlerFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_butler,null);
return view;
}
}
9.Fragment准备好了,那就开始搞事情了!编辑Activity_main,主要做了初始化控件,初始数据,设置适配器,绑定适配器
package com.example.qqazl001.smartbutler;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.example.qqazl001.smartbutler.fragment.ButlerFragment;
import com.example.qqazl001.smartbutler.fragment.GirlFragment;
import com.example.qqazl001.smartbutler.fragment.UserFragment;
import com.example.qqazl001.smartbutler.fragment.WechatFragment;
import com.example.qqazl001.smartbutler.ui.Setting_Activity;
import com.example.qqazl001.smartbutler.utils.L;
import com.example.qqazl001.smartbutler.utils.ShareUtil;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//初始化 TabLayout 和 ViewPager
private TabLayout mTabLayout;
private ViewPager mViewPager;
//悬浮窗
private FloatingActionButton fab_Setting;
//存放标题的List
private List<String> mTitle;
//存放内容发Fragment
private List<Fragment> mFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//去除阴影
getSupportActionBar().setElevation(0);
initData();
initView();
}
//初始化数据
private void initData() {
//初始化标题内容
mTitle = new ArrayList<>();
mTitle.add("智能管家");
mTitle.add("微信精选");
mTitle.add("美女社区");
mTitle.add("个人中心");
mFragment = new ArrayList<>();
mFragment.add(new ButlerFragment());
mFragment.add(new GirlFragment());
mFragment.add(new UserFragment());
mFragment.add(new WechatFragment());
}
//初始化View
private void initView(){
mTabLayout = findViewById(R.id.mTableLayout);
mViewPager = findViewById(R.id.mViewPager);
fab_Setting = findViewById(R.id.fab_setting);
//预加载
mViewPager.setOffscreenPageLimit(mFragment.size());
//设置按钮添加监听事件,监听跳转
fab_Setting.setOnClickListener(this);
//监听滑动,第一页不显示设置按钮
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if(position==0){
//首页则隐藏设置按钮
fab_Setting.setVisibility(View.GONE);
}else{
//显示设置按钮!
fab_Setting.setVisibility(View.VISIBLE);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
//设置适配器
mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
//选中的item
@Override
public Fragment getItem(int position) {
return mFragment.get(position);
}
//返回Item的个数
@Override
public int getCount() {
return mFragment.size();
}
//设置标题
@Override
public CharSequence getPageTitle(int position) {
return mTitle.get(position);
}
});
//绑定
mTabLayout.setupWithViewPager(mViewPager);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.fab_setting:
startActivity(new Intent(this, Setting_Activity.class));
}
}
}
自此,基础的我们已经实现了!
当然所有的Activity都需要去清单文件注册
<!--设置-->
<activity android:name=".ui.Setting_Activity"
android:label="@string/setting">
</activity>