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"
/>
************<?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}" >
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;//左外边距
}