android 实战练习 回归手机卫士 一 主界面布局

学习android一年多了,平常都是看着书,然后做些练习,总觉着学了后面,忘了前面,突然想做一个比较完整一点的项目,

于是不自觉地就想到了做这样一个应用,由于刚接触android没多久,程序肯定会存在或多或少的bug

不过现已大体为准吧,把所有的知识整体地复习和应用一遍也是不错的

以下是效果图

android 实战练习 回归手机卫士 一 主界面布局_第1张图片android 实战练习 回归手机卫士 一 主界面布局_第2张图片android 实战练习 回归手机卫士 一 主界面布局_第3张图片android 实战练习 回归手机卫士 一 主界面布局_第4张图片

 

今天就先上布局了,相关的业务逻辑后续再说吧

其实这也就是简单的fragment+viewpager的整体布局,然后零零散散地加上一些其他诸如ListView,GridView等布局

1.activity_main.xml主界面



    
        
        
        
    
    
    
    
    
        
        
        
         
    


2.activity代码

package cn.edu.gzu.BackPP.activities;

import java.io.File;
import java.util.ArrayList;
import cn.edu.gzu.BackPP.Fragment.IndexFragment;
import cn.edu.gzu.BackPP.Fragment.ManagerFragment;
import cn.edu.gzu.BackPP.Fragment.SettingFragment;
import cn.edu.gzu.BackPP.Fragment.YouhuaFragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements OnTouchListener{
	private Fragment indexFragment;// 获取到fragment实例
	private Fragment youhuaFragment;
	private Fragment managerFragment;
	private Fragment settingFragment;
	private ArrayList myFragments;// 由于存放fragment的集合
	private ViewPager viewPager;// 让fragment切换并装载的控件
	private TextView indexSwitcher;// 页面底部的四个切换组件
	private TextView youhusSwitcher;
	private TextView managerSwitcher;
	private TextView settingSwitcher;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		// 初始化加载fragment
		initFragment();
		// 通过id找到组件
		findViewById();
		//在外部存储sdcard上建立文件夹以便后续保存文件
		File fileDir=new File(Environment.getExternalStorageDirectory().getPath()+"/HuiGui/cache");
		if(!fileDir.exists()){
			fileDir.mkdirs();
		}
		
		View view = getWindow().getDecorView();
		view.setOnTouchListener(this);

		startServices();
	}
	


	/**
	 * 初始化加载fragment
	 */
	private void initFragment() {
		myFragments = new ArrayList();
		indexFragment = new IndexFragment();
		youhuaFragment = new YouhuaFragment();
		managerFragment = new ManagerFragment();
		settingFragment = new SettingFragment();

		// 将fragment实例添加到集合
		myFragments.add(indexFragment);
		myFragments.add(youhuaFragment);
		myFragments.add(managerFragment);
		myFragments.add(settingFragment);

		viewPager = (ViewPager) findViewById(R.id.activity_main_viewPager);
		viewPager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(),
				myFragments));

		viewPager.setCurrentItem(0);

		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				switch (arg0) {
				case 0:
					initTitleBG();
					indexSwitcher.setBackgroundResource(R.drawable.pressed_bg);
					break;
				case 1:
					initTitleBG();
					youhusSwitcher.setBackgroundResource(R.drawable.pressed_bg);
					break;
				case 2:
					initTitleBG();
					managerSwitcher.setBackgroundResource(R.drawable.pressed_bg);
					break;
				case 3:
					initTitleBG();
					settingSwitcher.setBackgroundResource(R.drawable.pressed_bg);
					break;
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

	};

	/**
	 * 通过id找到组件
	 */
	private void findViewById() {
		indexSwitcher = (TextView) findViewById(R.id.activity_main_index);
		youhusSwitcher = (TextView) findViewById(R.id.activity_main_youhua);
		managerSwitcher = (TextView) findViewById(R.id.activity_main_manager);
		settingSwitcher = (TextView) findViewById(R.id.activity_main_setting);
		indexSwitcher.setBackgroundColor(getResources().getColor(
				R.color.activity_mian_bottom_selected));

		ViewOnClickListener viewOnClickListener = new ViewOnClickListener();
		indexSwitcher.setOnClickListener(viewOnClickListener);
		youhusSwitcher.setOnClickListener(viewOnClickListener);
		managerSwitcher.setOnClickListener(viewOnClickListener);
		settingSwitcher.setOnClickListener(viewOnClickListener);
	}

	/**
	 * initHomeTitle background rescource
	 */
	private void initTitleBG() {
		indexSwitcher.setBackgroundColor(getResources().getColor(
				R.color.activity_mian_title));
		youhusSwitcher.setBackgroundColor(getResources().getColor(
				R.color.activity_mian_title));
		managerSwitcher.setBackgroundColor(getResources().getColor(
				R.color.activity_mian_title));
		settingSwitcher.setBackgroundColor(getResources().getColor(
				R.color.activity_mian_title));
	}

	/**
	 * 组件监听其
	 */
	private class ViewOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.activity_main_index:
				viewPager.setCurrentItem(0);
				break;
			case R.id.activity_main_youhua:
				viewPager.setCurrentItem(1);
				break;
			case R.id.activity_main_manager:
				viewPager.setCurrentItem(2);
				break;
			case R.id.activity_main_setting:
				viewPager.setCurrentItem(3);
				break;
			
			}
		}
	}	
}


3.建立相应的fragment实例,并加载相应的布局文件,这里只列举其中一页,其他页可相应建立

package cn.edu.gzu.BackPP.Fragment;

import cn.edu.gzu.BackPP.Dao.Const;
import cn.edu.gzu.BackPP.activities.R;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

public class SettingFragment extends Fragment implements OnClickListener {

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_main_fragment_setting,
				container, false);
		return view;
	}
}


4.xml布局(普通布局文件)



	 
	    
	         
	         
	    
	    
	    
	         
	         
	    
	    
	    
	    
    
    
	         
	         
	    
	    
	    
	         
	         
	    
	    
	    
	    
    
    
	         
	         
	    
	    
	    
	         
	         
	    
	    
	    
	         
	         
	    
	    
	    
	    
	    
     
    
	         
	         
	    
	    
	    
	         
	         
	    
	    
	    
	         
	         
	    
	    
    


5.fragment适配器

package cn.edu.gzu.BackPP.adapter;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyFragmentAdapter extends FragmentPagerAdapter{
	private ArrayList myFragments;
	
	public MyFragmentAdapter(FragmentManager fm) {
		super(fm);
	}
	public MyFragmentAdapter(FragmentManager fragmentManager,ArrayList myFragments) {
		super(fragmentManager);
		this.myFragments=myFragments;
	}

	@Override
	public Fragment getItem(int arg0) {
		return myFragments.get(arg0);
	}

	@Override
	public int getCount() {
		return myFragments.size();
	}
	
	
	@Override
	public int getItemPosition(Object object) {
		return super.getItemPosition(object);
	}

}


这样就可以实现能左右滑动的主界面了,效果基本上实现如效果图所示,由于其他三页也都是差不多的布局,有时间的话,后续再贴出来

可能界面不是很漂亮,不过目的也就是为了练习,大部分图片都在网上搜的,没办法了

 

 

你可能感兴趣的:(android布局)