导航栏


package com.example.test13;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;


public class MainActivity extends Activity {
private TabHost tabhost;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tabhost = (TabHost) findViewById(R.id.tabhost);
tabhost.setup();//找到TabWidget和FrameLayout

//添加标签页
TabSpec tab1 = tabhost.newTabSpec("tab1");
// tab1.setIndicator("首页",getResources().getDrawable(R.drawable.i1));
tab1.setIndicator(createView("首页"));
//指定标签的内容
tab1.setContent(R.id.line1);
tabhost.addTab(tab1);

//添加标签页
TabSpec tab2 = tabhost.newTabSpec("tab2");
// tab2.setIndicator("第二页",getResources().getDrawable(R.drawable.i2));
tab2.setIndicator(createView("第二页"));
//指定标签的内容
tab2.setContent(R.id.line2);
tabhost.addTab(tab2);

//添加标签页
TabSpec tab3 = tabhost.newTabSpec("tab3");
// tab3.setIndicator("第三页",getResources().getDrawable(R.drawable.i7));
tab3.setIndicator(createView("第三页"));
//指定标签的内容
tab3.setContent(R.id.line3);
tabhost.addTab(tab3);
}
private View createView(String text){
View view = View.inflate(this, R.layout.tab, null);
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
tv_title.setText(text);
return view;
}

}




<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


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


        <TabWidget
             android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>


        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >
            <!-- 首页 -->
            <LinearLayout 
                android:id="@+id/line1"
                android:layout_width="match_parent"
                  android:layout_height="match_parent"
               
                >
                <TextView 
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="首页"
                    android:textSize="30sp"
                    android:gravity="center"
                    />
                
            </LinearLayout>
            <!-- 第二页-->
            <LinearLayout 
                android:id="@+id/line2"
                android:layout_width="match_parent"
                  android:layout_height="match_parent"
               
                >
                <TextView 
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="第二页"
                    android:textSize="30sp"
                    android:gravity="center"
                    />
                
            </LinearLayout>
            <!-- 第三页 -->
            <LinearLayout 
                android:id="@+id/line3"
                android:layout_width="match_parent"
                  android:layout_height="match_parent"
               
                >
                <TextView 
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="第三页"
                    android:textSize="30sp"
                    android:gravity="center"
                    />
                
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>


</TabHost>


_________________________________________________________________________________________________________________


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="false"
android:drawable="@drawable/point_shape"></item>
<item android:state_enabled="true"
android:drawable="@drawable/point_shape2"></item>

</selector>
************point_shape.xml************
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"

>

<size

    android:width="8dp"

    android:height="8dp"

/>

<solid

        android:color="#440000000"

/>

</shape>
************<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=" http://schemas.android.com/apk/res/android"
android:shape="oval"

android:shape="oval"

>
<size 
    android:width="8dp"
    android:height="8dp"
    />
<solid 
    android:color="#44000000"
    />

</shape>
**************point_shape2.xml*************
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"

>
<size 
    android:width="8dp"
    android:height="8dp"
    />
<solid 
    android:color="#ff0000"
    />

</shape>
在******activity.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"
tools:context="${relativePackage}.${activityClass}" >


<android.support.v4.view.ViewPager 
android:id="@+id/vp_main"
android:layout_width="match_parent"
android:layout_height="180dp" 
/> <LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content" 
android:layout_alignBottom="@id/vp_main" 
android:background="#44000000" 
android:orientation="vertical" > 
  <TextView android:id="@+id/tv_title"
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:gravity="center_horizontal" android:padding="3dp"
android:text="广告标题" android:textColor="#ffffff" />
<LinearLayout 
android:id="@+id/ll_point_group" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:layout_gravity="center_horizontal" >
</LinearLayout> </LinearLayout>



package com.example.test13_viewpager;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
private ViewPager vp_main;
private TextView tv_title;
private LinearLayout ll_point_group;
private ArrayList<ImageView> imageViews;
private int prePosition=0;//上一次高亮显示的位置
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg){
int item = vp_main.getCurrentItem() + 1;
vp_main.setCurrentItem(item);
//延迟发消息
handler.sendEmptyMessageDelayed(0, 3000);
};
};
//是否已经拖拽
private boolean isDragging =false;
//图片资源id
private int[] imgeId={R.drawable.a,
R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
//图片标题集合
private String[] imgDescrintions={
"周末大放送","家电买一送一","京东火锅节","预约新机","有范"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//ListView的使用
//1.在布局文件中定义ViewPager
//2.在代码中实例化viewpager
setContentView(R.layout.activity_main);
vp_main=(ViewPager) findViewById(R.id.vp_main);
tv_title=(TextView) findViewById(R.id.tv_title);
ll_point_group=(LinearLayout) findViewById(R.id.ll_point_group);
//3.准备数据
imageViews=new ArrayList<ImageView>();
for (int i = 0; i < imgeId.length; i++) {
ImageView imageView = new ImageView(this);
//设置背景图片
imageView.setBackgroundResource(imgeId[i]);
//添加到集合
imageViews.add(imageView);
//添加小圆点
ImageView point=new ImageView(this);
//设置背景资源
point.setBackgroundResource(R.drawable.point_selector);
//间距
LinearLayout.LayoutParams params=new
LinearLayout.LayoutParams(8,8);
if (i==0) {
point.setEnabled(true);//红色
}else{
point.setEnabled(false);//灰色
params.leftMargin=8;//左外边距
}


 point.setLayoutParams(params);
 ll_point_group.addView(point); } 
  / /4.设置适配器(pagerAdapter)
 Item布局 绑定数据 vp_main.setAdapter( new MyAdapter());
  //设置监听 viewPager页面的改变 
 vp_main.setOnPageChangeListener( new MyPageChangeListener());
  //设置中间位置 保证是imageViews的整数倍
int item=Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % imageViews.size();
 vp_main.setCurrentItem(item);
//不断的加一 加载 tv_title.setText(imgDescrintions[prePosition]); 
//发送消息 handler.sendEmptyMessageDelayed( 0, 3000);}

class MyPageChangeListener implements OnPageChangeListener
  /** 
 当页面滚动状态变化的时候回调这个方法 静止-》滑动 或 滑动 -> 静止 或 静止--》拖拽
 * */ 
  @Override public void onPageScrollStateChanged(int state)
  if (state==ViewPager.SCROLL_STATE_DRAGGING) {
//拖拽 
 isDragging= true; }   else if (state==ViewPager.SCROLL_STATE_SETTLING){
//滚动 
 } else if (state==ViewPager.SCROLL_STATE_IDLE&&isDragging ){ 
  //静止并拖拽 isDragging= false
  //重新发送事先移除之前的消息
handler.removeCallbacksAndMessages( null);
 handler.sendEmptyMessageDelayed( 0, 3000); } } 
  /** 当前页面滚动了的时候回调的方法 * 
position 当前页面的位置 *
positionOffset 滑动页面的半分比 * positionOffsetPixels 在屏幕上滑动的像素
 */ 
  @Override 
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } 
  /** 
 当某个页面被选中了的时候 postion 被选中页面的位置
 */


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值