selector 以及导航栏

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <!-- 点击状态下的效果 -->  
  4.     <item android:state_pressed="true">  
  5.         <shape>  
  6.             <!-- 设置背景填充色为黑色 -->  
  7.             <solid android:color="@color/black"/>  
  8.             <!-- 设置边框宽度为1dp,边框颜色为白色 -->  
  9.             <stroke android:width="1dp" android:color="@color/white" />  
  10.             <!-- 设置按钮圆角半径为5dp -->  
  11.             <corners android:radius="5dp" />  
  12.             <!-- 设置按钮中间文字距上下左右都为10dp的间距 -->  
  13.             <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />  
  14.         </shape>  
  15.     </item>  
  16.     <!-- 正常状态下(非点击)的效果 -->  
  17.     <item android:state_pressed="false">  
  18.         <shape>  
  19.             <!-- 设置背景填充色为白色 -->  
  20.             <solid android:color="@color/white"/>  
  21.             <!-- 设置边框宽度为1dp,边框颜色为黑色 -->  
  22.             <stroke android:width="1dp" android:color="@color/black" />  
  23.             <!-- 设置按钮圆角半径为5dp -->  
  24.             <corners android:radius="5dp" />  
  25.             <!-- 设置按钮中间文字距上下左右都为10dp的间距 -->  
  26.             <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />  
  27.         </shape>  
  28.     </item>  
  29.   

  1. </selector>  



  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="45dp"  
  10.         android:background="#0E6DB0"  
  11.         android:gravity="center"  
  12.         android:orientation="vertical" >  
  13.   
  14.         <TextView  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:layout_gravity="center"  
  18.             android:text="@string/app_name"  
  19.             android:textColor="#ffffff"  
  20.             android:textSize="20sp"  
  21.             android:textStyle="bold" />  
  22.     </LinearLayout>  
  23.   
  24.     <android.support.v4.view.ViewPager  
  25.         android:id="@+id/viewPager"  
  26.         android:layout_width="match_parent"  
  27.         android:layout_height="0dp"  
  28.         android:layout_weight="1" >  
  29.     </android.support.v4.view.ViewPager>  
  30.   
  31.     <LinearLayout  
  32.         android:layout_width="match_parent"  
  33.         android:layout_height="55dp"  
  34.         android:background="#0E6DB0"  
  35.         android:orientation="horizontal" >  
  36.   
  37.         <LinearLayout  
  38.             android:id="@+id/llChat"  
  39.             android:layout_width="0dp"  
  40.             android:layout_height="match_parent"  
  41.             android:layout_weight="1"  
  42.             android:gravity="center"  
  43.             android:orientation="vertical" >  
  44.   
  45.             <ImageView  
  46.                 android:id="@+id/ivChat"  
  47.                 android:layout_width="wrap_content"  
  48.                 android:layout_height="wrap_content"  
  49.                 android:background="#00000000"  
  50.                 android:src="@drawable/tab_chat" />  
  51.   
  52.             <TextView  
  53.                 android:id="@+id/tvChat"  
  54.                 android:layout_width="wrap_content"  
  55.                 android:layout_height="wrap_content"  
  56.                 android:text="微信"  
  57.                 android:textColor="@drawable/tab_textview" />  
  58.         </LinearLayout>  
  59.   
  60.         <LinearLayout  
  61.             android:id="@+id/llFriends"  
  62.             android:layout_width="0dp"  
  63.             android:layout_height="match_parent"  
  64.             android:layout_weight="1"  
  65.             android:gravity="center"  
  66.             android:orientation="vertical" >  
  67.   
  68.             <ImageView  
  69.                 android:id="@+id/ivFriends"  
  70.                 android:layout_width="wrap_content"  
  71.                 android:layout_height="wrap_content"  
  72.                 android:background="#00000000"  
  73.                 android:src="@drawable/tab_friends" />  
  74.   
  75.             <TextView  
  76.                 android:id="@+id/tvFriends"  
  77.                 android:layout_width="wrap_content"  
  78.                 android:layout_height="wrap_content"  
  79.                 android:text="朋友"  
  80.                 android:textColor="@drawable/tab_textview" />  
  81.         </LinearLayout>  
  82.   
  83.         <LinearLayout  
  84.             android:id="@+id/llContacts"  
  85.             android:layout_width="0dp"  
  86.             android:layout_height="match_parent"  
  87.             android:layout_weight="1"  
  88.             android:gravity="center"  
  89.             android:orientation="vertical" >  
  90.   
  91.             <ImageView  
  92.                 android:id="@+id/ivContacts"  
  93.                 android:layout_width="wrap_content"  
  94.                 android:layout_height="wrap_content"  
  95.                 android:background="#00000000"  
  96.                 android:src="@drawable/tab_contacts" />  
  97.   
  98.             <TextView  
  99.                 android:id="@+id/tvContacts"  
  100.                 android:layout_width="wrap_content"  
  101.                 android:layout_height="wrap_content"  
  102.                 android:text="通讯录"  
  103.                 android:textColor="@drawable/tab_textview" />  
  104.         </LinearLayout>  
  105.   
  106.         <LinearLayout  
  107.             android:id="@+id/llSettings"  
  108.             android:layout_width="0dp"  
  109.             android:layout_height="match_parent"  
  110.             android:layout_weight="1"  
  111.             android:gravity="center"  
  112.             android:orientation="vertical" >  
  113.   
  114.             <ImageView  
  115.                 android:id="@+id/ivSettings"  
  116.                 android:layout_width="wrap_content"  
  117.                 android:layout_height="wrap_content"  
  118.                 android:background="#00000000"  
  119.                 android:src="@drawable/tab_setting" />  
  120.   
  121.             <TextView  
  122.                 android:id="@+id/tvSettings"  
  123.                 android:layout_width="wrap_content"  
  124.                 android:layout_height="wrap_content"  
  125.                 android:text="设置"  
  126.                 android:textColor="@drawable/tab_textview" />  
  127.         </LinearLayout>  
  128.     </LinearLayout>  
  129.   
  130. </LinearLayout>  

  1. public class MyPagerAdapter extends PagerAdapter {  
  2.   
  3.   
  4.     /* 
  5.      * 使用 pagerAdapter  注意几点: 
  6.      * 重写方法的时候是 含有 ViewGroup的方法 : 
  7.      * 适合用在 使用 layout 布局实现 
  8.      * instantiateItem(ViewGroup container, int position) 
  9.      * destroyItem(ViewGroup container, int position, Object object) 
  10.      *  
  11.      */  
  12.     private List<View> views;  
  13.     private List<String> titiles;  
  14.       
  15.     public MyPagerAdapter(List<View> views,List<String> titles) {  
  16.         this.views=views;  
  17.         this.titiles=titles;  
  18.     }  
  19.       
  20.     /** 
  21.      * 返回 页卡 的数量 
  22.      */  
  23.     @Override  
  24.     public int getCount() {  
  25.         // TODO Auto-generated method stub  
  26.         return views.size();  
  27.     }  
  28.   
  29.     /** 
  30.      *  判断 是 view 是否来自对象 
  31.      */  
  32.     @Override  
  33.     public boolean isViewFromObject(View arg0, Object arg1) {  
  34.         // TODO Auto-generated method stub  
  35.         return arg0==arg1;  
  36.     }  
  37.       
  38.     /** 
  39.      * 实例化 一个 页卡  
  40.      */  
  41.      @Override  
  42.     public Object instantiateItem(ViewGroup container, int position) {  
  43.         // 添加一个 页卡  
  44.            
  45.          container.addView(views.get(position));  
  46.            
  47.         return views.get(position);  
  48.     }  
  49.       
  50.     /** 
  51.      * 销毁 一个 页卡 
  52.      */  
  53.     @Override  
  54.     public void destroyItem(ViewGroup container, int position, Object object) {  
  55.         // 删除  
  56.         container.removeView(views.get(position));  
  57.     }  
  58.   
  59.     /** 
  60.      *  重写 标题的 方法 
  61.      */  
  62.     @Override  
  63.     public CharSequence getPageTitle(int position) {  
  64.         // 给页面添加标题  
  65.         return titiles.get(position);  
  66.     }  
  67.       
  68. }  
  1. package com.yao.tab01;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.support.v4.view.ViewPager;  
  9. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  10. import android.view.LayoutInflater;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.view.Window;  
  14. import android.widget.ImageView;  
  15. import android.widget.LinearLayout;  
  16. import android.widget.TextView;  
  17.   
  18. public class MainActivity extends Activity implements OnClickListener {  
  19.   
  20.     private List<View> views = new ArrayList<View>();  
  21.     private ViewPager viewPager;  
  22.     private LinearLayout llChat, llFriends, llContacts, llSettings;  
  23.     private ImageView ivChat, ivFriends, ivContacts, ivSettings, ivCurrent;  
  24.     private TextView tvChat, tvFriends, tvContacts, tvSettings, tvCurrent;  
  25.   
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  30.         setContentView(R.layout.activity_main);  
  31.   
  32.         initView();  
  33.   
  34.         initData();  
  35.     }  
  36.   
  37.     private void initView() {  
  38.         viewPager = (ViewPager) findViewById(R.id.viewPager);  
  39.   
  40.         llChat = (LinearLayout) findViewById(R.id.llChat);  
  41.         llFriends = (LinearLayout) findViewById(R.id.llFriends);  
  42.         llContacts = (LinearLayout) findViewById(R.id.llContacts);  
  43.         llSettings = (LinearLayout) findViewById(R.id.llSettings);  
  44.   
  45.         llChat.setOnClickListener(this);  
  46.         llFriends.setOnClickListener(this);  
  47.         llContacts.setOnClickListener(this);  
  48.         llSettings.setOnClickListener(this);  
  49.   
  50.         ivChat = (ImageView) findViewById(R.id.ivChat);  
  51.         ivFriends = (ImageView) findViewById(R.id.ivFriends);  
  52.         ivContacts = (ImageView) findViewById(R.id.ivContacts);  
  53.         ivSettings = (ImageView) findViewById(R.id.ivSettings);  
  54.   
  55.         tvChat = (TextView) findViewById(R.id.tvChat);  
  56.         tvFriends = (TextView) findViewById(R.id.tvFriends);  
  57.         tvContacts = (TextView) findViewById(R.id.tvContacts);  
  58.         tvSettings = (TextView) findViewById(R.id.tvSettings);  
  59.   
  60.         ivChat.setSelected(true);  
  61.         tvChat.setSelected(true);  
  62.         ivCurrent = ivChat;  
  63.         tvCurrent = tvChat;  
  64.   
  65.         viewPager.setOnPageChangeListener(new OnPageChangeListener() {  
  66.   
  67.             @Override  
  68.             public void onPageSelected(int position) {  
  69.                 changeTab(position);  
  70.             }  
  71.   
  72.             @Override  
  73.             public void onPageScrolled(int arg0, float arg1, int arg2) {  
  74.   
  75.             }  
  76.   
  77.             @Override  
  78.             public void onPageScrollStateChanged(int arg0) {  
  79.   
  80.             }  
  81.         });  
  82.     }  
  83.   
  84.     private void initData() {  
  85.         LayoutInflater mInflater = LayoutInflater.from(this);  
  86.         View tab01 = mInflater.inflate(R.layout.tab01, null);  
  87.         View tab02 = mInflater.inflate(R.layout.tab02, null);  
  88.         View tab03 = mInflater.inflate(R.layout.tab03, null);  
  89.         View tab04 = mInflater.inflate(R.layout.tab04, null);  
  90.         views.add(tab01);  
  91.         views.add(tab02);  
  92.         views.add(tab03);  
  93.         views.add(tab04);  
  94.   
  95.         MyPagerAdapter adapter = new MyPagerAdapter(views);  
  96.         viewPager.setAdapter(adapter);  
  97.     }  
  98.   
  99.     @Override  
  100.     public void onClick(View v) {  
  101.         changeTab(v.getId());  
  102.     }  
  103.   
  104.     private void changeTab(int id) {  
  105.         ivCurrent.setSelected(false);  
  106.         tvCurrent.setSelected(false);  
  107.         switch (id) {  
  108.         case R.id.llChat:  
  109.             viewPager.setCurrentItem(0);  
  110.         case 0:  
  111.             ivChat.setSelected(true);  
  112.             ivCurrent = ivChat;  
  113.             tvChat.setSelected(true);  
  114.             tvCurrent = tvChat;  
  115.             break;  
  116.         case R.id.llFriends:  
  117.             viewPager.setCurrentItem(1);  
  118.         case 1:  
  119.             ivFriends.setSelected(true);  
  120.             ivCurrent = ivFriends;  
  121.             tvFriends.setSelected(true);  
  122.             tvCurrent = tvFriends;  
  123.             break;  
  124.         case R.id.llContacts:  
  125.             viewPager.setCurrentItem(2);  
  126.         case 2:  
  127.             ivContacts.setSelected(true);  
  128.             ivCurrent = ivContacts;  
  129.             tvContacts.setSelected(true);  
  130.             tvCurrent = tvContacts;  
  131.             break;  
  132.         case R.id.llSettings:  
  133.             viewPager.setCurrentItem(3);  
  134.         case 3:  
  135.             ivSettings.setSelected(true);  
  136.             ivCurrent = ivSettings;  
  137.             tvSettings.setSelected(true);  
  138.             tvCurrent = tvSettings;  
  139.             break;  
  140.         default:  
  141.             break;  
  142.         }  
  143.     }  
  144.   
  145. }  




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值