一 . 自定義標(biāo)簽
二 . ViewPager
三. ScrollView
----------------效果圖----如下---------------
我們定義一個(gè)selector選擇器
-----------bg.xml-------------
<item android:state_selected="true"
android:drawable="@drawable/bg_selected"
></item>
<item android:drawable="@drawable/bg_normal"></item>
在 activity.xml-----------------------代碼如下
···
<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" >
<!-- 首頁(yè) -->
<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="首頁(yè)"
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>
···
在定義一個(gè)tab.xml---------------------
*********代碼如下****************
···
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg"
>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="首頁(yè)"
android:textColor="@android:color/white"
android:textSize="22sp"
/>
</LinearLayout>
···
MainActivity***代碼如下************
···
package com.example.app08_tabhost;
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
//添加標(biāo)簽頁(yè)
TabSpec tab1 = tabhost.newTabSpec("tab1");
//指定標(biāo)簽
// tab1.setIndicator("首頁(yè)",getResources().getDrawable(R.drawable.i1));
tab1.setIndicator(createView("首頁(yè)"));
//指定標(biāo)簽頁(yè)內(nèi)容
tab1.setContent(R.id.line1);
tabhost.addTab(tab1);
//添加標(biāo)簽頁(yè)
TabSpec tab2 = tabhost.newTabSpec("tab2");
//指定標(biāo)簽
tab2.setIndicator(createView("第二頁(yè)"));
// tab2.setIndicator("中間",getResources().getDrawable(R.drawable.i2));
//指定標(biāo)簽頁(yè)內(nèi)容
tab2.setContent(R.id.line2);
tabhost.addTab(tab2);
//添加標(biāo)簽頁(yè)
TabSpec tab3 = tabhost.newTabSpec("tab3");
//指定標(biāo)簽
tab3.setIndicator(createView("第三頁(yè)"));
// tab3.setIndicator("最后頁(yè)",getResources().getDrawable(R.drawable.i7));
//指定標(biāo)簽頁(yè)內(nèi)容
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;
}
}
···
************效果圖*************
我們定義一個(gè)選擇器 selector和 shape
*********代碼如下-----------
************point_selector.xml************
<?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="#44000000"
/>
</shape>
************<?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="#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="廣告標(biāo)題"
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>
</RelativeLayout>
···
在MainActivity中***********代碼如下********
···
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);
//延遲發(fā)消息
handler.sendEmptyMessageDelayed(0, 3000);
};
};
//是否已經(jīng)拖拽
private boolean isDragging =false;
//圖片資源id
private int[] imgeId={R.drawable.a,
R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
//圖片標(biāo)題集合
private String[] imgDescrintions={
"周末大放送","家電買(mǎi)一送一","京東火鍋節(jié)","預(yù)約新機(jī)","有范"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//ListView的使用
//1.在布局文件中定義ViewPager
//2.在代碼中實(shí)例化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.準(zhǔn)備數(shù)據(jù)
imageViews=new ArrayList<ImageView>();
for (int i = 0; i < imgeId.length; i++) {
ImageView imageView = new ImageView(this);
//設(shè)置背景圖片
imageView.setBackgroundResource(imgeId[i]);
//添加到集合
imageViews.add(imageView);
//添加小圓點(diǎn)
ImageView point=new ImageView(this);
//設(shè)置背景資源
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.設(shè)置適配器(pagerAdapter) Item布局 綁定數(shù)據(jù)
vp_main.setAdapter(new MyAdapter());
//設(shè)置監(jiān)聽(tīng)viewPager頁(yè)面的改變
vp_main.setOnPageChangeListener(new MyPageChangeListener());
//設(shè)置中間位置 保證是imageViews的整數(shù)倍
int item=Integer.MAX_VALUE / 2
- Integer.MAX_VALUE /2 % imageViews.size();
vp_main.setCurrentItem(item);//不斷的加一 加載
tv_title.setText(imgDescrintions[prePosition]);
//發(fā)送消息
handler.sendEmptyMessageDelayed(0, 3000);
}
class MyPageChangeListener implements OnPageChangeListener{
/**
當(dāng)頁(yè)面滾動(dòng)狀態(tài)變化的時(shí)候回調(diào)這個(gè)方法
靜止-》滑動(dòng) 或 滑動(dòng) -> 靜止 或 靜止--》拖拽
*
*/
@Override
public void onPageScrollStateChanged(int state) {
if (state==ViewPager.SCROLL_STATE_DRAGGING) {//拖拽
isDragging=true;
} else if (state==ViewPager.SCROLL_STATE_SETTLING){//滾動(dòng)
}else if (state==ViewPager.SCROLL_STATE_IDLE&&isDragging ){
//靜止并拖拽
isDragging=false;
//重新發(fā)送事先移除之前的消息
handler.removeCallbacksAndMessages(null);
handler.sendEmptyMessageDelayed(0, 3000);
}
}
/**
* 當(dāng)前頁(yè)面滾動(dòng)了的時(shí)候回調(diào)的方法
* position 當(dāng)前頁(yè)面的位置
* positionOffset 滑動(dòng)頁(yè)面的半分比
* positionOffsetPixels 在屏幕上滑動(dòng)的像素
*/
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
/**
當(dāng)某個(gè)頁(yè)面被選中了的時(shí)候
postion 被選中頁(yè)面的位置
*/
@Override// 當(dāng)某個(gè)頁(yè)面被選中了的時(shí)候
public void onPageSelected(int position) {
int realPosition =position % imageViews.size();
//設(shè)置對(duì)頁(yè)面的文本信息
tv_title.setText(imgDescrintions[realPosition]);
//把上一個(gè)高亮的設(shè)置灰色
ll_point_group.getChildAt(realPosition)
.setEnabled(false);
//當(dāng)前的設(shè)置為紅色
ll_point_group.getChildAt(realPosition)
.setEnabled(true);
prePosition=realPosition;
}
}
class MyAdapter extends PagerAdapter{
@Override//得到圖片的總數(shù)
public int getCount() {
return Integer.MAX_VALUE;//int類(lèi)型的最大值
}
@Override//比較 View 和Object 是否是同一個(gè)實(shí)例
//view 頁(yè)面 object 是instantiateItem返回的結(jié)果
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
/**類(lèi)似getView的方法
* container viewPager自身
* position 當(dāng)前實(shí)例化頁(yè)面的位置(下標(biāo))
*
* */
@Override//實(shí)例化
public Object instantiateItem(ViewGroup container, int position) {
int realPosition = position % imageViews.size();
ImageView imageView = imageViews.get(realPosition);
container.addView(imageView);//添加到ViewPager
//觸摸監(jiān)聽(tīng)
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {//獲取動(dòng)作
case MotionEvent.ACTION_DOWN://按下
//移除消息
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_MOVE://移動(dòng)
//移除消息
// handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_CANCEL://取消
//移除消息
handler.removeCallbacksAndMessages(null);
handler.sendEmptyMessageDelayed(0, 3000);
break;
case MotionEvent.ACTION_UP://抬起
handler.removeCallbacksAndMessages(null);
handler.sendEmptyMessageDelayed(0, 3000);
break;
}
return false;
}
});
imageView.setTag(position);//設(shè)置下標(biāo)
//給圖片設(shè)置監(jiān)聽(tīng)
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position =(Integer) v.getTag() % imageViews.size();
//獲取文字
String text= imgDescrintions[position];
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
// Log.e("TAG","instantiateItem---->"+position+"imageView-->"+imageView);
return imageView;
}
@Override//釋放資源 1.container值得是Viewpager自身 2.下標(biāo)译仗,要釋放的位置 3.object 要釋放的頁(yè)面
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
···
***************ScrollView**************
效果圖*******************如下
scroll.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}" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="里面文字自己定義" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
···
在 MainActivity中**************
findViewById ().setOnClickListener(this);
public void onClick(View v){
startActivity(new Intent (MainActivity.this,SecondActivity.class))
}