Android高仿360安全卫士--布局篇

原文:http://blog.csdn.net/wangjinyu501/article/details/8083373

这个Demo是可以左右滑动的,并且可以在布局文件中添加组件点击事件。主要是利用ViewPager类来实现的。

Activity程序:

package com.example.test;

import java.util.ArrayList;
import java.util.List;


import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;

public class TActivity extends Activity {
	
	private ViewPager mViewPager;
	private LinearLayout lin1, lin2;
	private Context cxt;
	private ViewPagerAdapter mAdapter;
	private List<View> mListViews;
	private LayoutInflater mInflater;
	boolean result = true;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        cxt = this;
        
        mListViews = new ArrayList<View>();
        mInflater = getLayoutInflater();
        mListViews.add(mInflater.inflate(R.layout.tab1, null));
        mListViews.add(mInflater.inflate(R.layout.tab2, null));
        
        mAdapter = new ViewPagerAdapter(mListViews);
        mViewPager = (ViewPager)findViewById(R.id.mViewPager);
        mViewPager.setAdapter(mAdapter);
        
    }
    /*
     * ViewPager适配器类
     * 主要用到两个函数:instantiateItem和destroyedItem
     * instantiateItem主要实现新的画面的添加
     * destroyedItem主要实现老的画面去处
     */
    private class ViewPagerAdapter extends PagerAdapter{

    	private List<View> list;
    	
    	public ViewPagerAdapter(List<View> list) {
			// TODO Auto-generated constructor stub
    		this.list = list;
		}
    	
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		
		
		@Override
		public Object instantiateItem(View container, int position) {
			// TODO Auto-generated method stub
			((ViewPager)container).addView(list.get(position),0);
			if (position == 0) {
				ImageView download_btn = (ImageView)container.findViewById(R.id.download_btn);
				download_btn.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						new AlertDialog.Builder(TActivity.this)  
                        .setTitle("说明")  
                        .setMessage("单个页卡内按钮事件测试")  
                        .setNegativeButton("确定",  
                               new DialogInterface.OnClickListener() {  
                                    public void onClick(  
                                            DialogInterface dialog,  
                                            int which) {  
                                   }  
                            }).show();
					}
				});
			}
			return list.get(position);
		}

		
		
		@Override
		public void destroyItem(View container, int position, Object object) {
			// TODO Auto-generated method stub
			((ViewPager)container).removeView(list.get(position));
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}
    	
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    
}

 

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<!-- 千万要把路径写好,第一次路径不对,也没有报错,调了老久 -->
<android.support.v4.view.ViewPager
    android:id="@+id/mViewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>

</RelativeLayout>


tab1.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
     android:id="@+id/lin1"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:baselineAligned="false"
     android:background="@drawable/rootblock_default_bg"
     android:orientation="horizontal"
     android:paddingBottom="30dp"
     android:paddingLeft="10dp"
     android:paddingTop="30dp" xmlns:android="http://schemas.android.com/apk/res/android">

     <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:layout_weight="1"
         android:orientation="vertical" >

         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="horizontal" >

             <LinearLayout
                 android:layout_width="108dp"
                 android:layout_height="108dp"
                 android:background="#FF7F24" >
             </LinearLayout>

             <LinearLayout
                 android:layout_width="108dp"
                 android:layout_height="108dp"
                 android:layout_marginLeft="5dp"
                 android:background="#FF7F24" >
             </LinearLayout>
         </LinearLayout>

         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="5dp"
             android:orientation="horizontal" >

             <LinearLayout
                 android:layout_width="108dp"
                 android:layout_height="108dp"
                 android:background="#3399ff" >
             </LinearLayout>

             <LinearLayout
                 android:layout_width="108dp"
                 android:layout_height="108dp"
                 android:layout_marginLeft="5dp"
                 android:background="#3399ff" >
             </LinearLayout>
         </LinearLayout>

         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="5dp"
             android:orientation="horizontal" >

             <LinearLayout
                 android:layout_width="108dp"
                 android:layout_height="108dp"
                 android:background="#3399ff" >
             </LinearLayout>

             <LinearLayout
                 android:layout_width="108dp"
                 android:layout_height="108dp"
                 android:layout_marginLeft="5dp"
                 android:background="#3399ff" >
             </LinearLayout>
         </LinearLayout>

         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="5dp"
             android:orientation="horizontal" >

             <LinearLayout
                 android:layout_width="108dp"
                 android:layout_height="108dp"
                 android:background="#953399ff" >
             </LinearLayout>

             <LinearLayout
                 android:layout_width="108dp"
                 android:layout_height="108dp"
                 android:layout_marginLeft="5dp"
                 android:background="#953399ff" >
             </LinearLayout>
         </LinearLayout>
     </LinearLayout>


     <RelativeLayout
         android:layout_width="wrap_content"
         android:layout_height="match_parent" 
         android:layout_weight="1">

         <ImageView
             android:id="@+id/etra_btn"
             android:layout_width="36dp"
             android:layout_height="36dp"
             android:layout_marginBottom="10dp"
             android:layout_alignParentBottom="true"
             android:src="@drawable/rootblock_icon_add_bg" />
         
         <ImageView
             android:id="@+id/set_btn"
             android:layout_width="36dp"
             android:layout_height="36dp"
             android:layout_above="@id/etra_btn"
             android:layout_marginBottom="10dp"
             android:src="@drawable/rootblock_icon_set_bg" />
         
         <ImageView
             android:id="@+id/delete_btn"
             android:layout_width="36dp"
             android:layout_height="36dp"
             android:layout_above="@id/set_btn"
             android:layout_marginBottom="10dp"
             android:src="@drawable/rootblock_icon_clear_bg" />
         
         <ImageView
             android:id="@+id/download_btn"
             android:layout_width="36dp"
             android:layout_height="36dp"
             android:layout_above="@id/delete_btn"
             android:layout_marginBottom="10dp"
             android:src="@drawable/rootblock_icon_download_bg" />
     </RelativeLayout>
 </LinearLayout>


tab2.xml布局tab1.xml

 工程文件:http://download.csdn.net/detail/zhaoshiqing7/4740665

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值