最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

自定義RadioButton和ViewPager實(shí)現(xiàn)TabHost帶滑動的頁卡效果

 更新時(shí)間:2013年01月18日 14:31:41   作者:  
在工作中又很多需求都不是android系統(tǒng)自帶的控件可以達(dá)到效果的所以這個時(shí)候就要自定義控件來達(dá)到效果:使用自定義RadioButton和ViewPager實(shí)現(xiàn)TabHost帶滑動的頁卡效果

在工作中又很多需求都不是android系統(tǒng)自帶的控件可以達(dá)到效果的,內(nèi)置的TabHost就是,只能達(dá)到簡單的效果 ,所以這個時(shí)候就要自定義控件來達(dá)到效果:這個效果就是: 使用自定義RadioButton和ViewPager實(shí)現(xiàn)TabHost帶滑動的頁卡效果。

這篇文章技術(shù)含量一般,大家別見笑。源碼我以測試,在底部可下載。
好了先上效果圖:

以下是實(shí)現(xiàn)步驟:        
1、準(zhǔn)備自定義RadioButton控件的樣式圖片等,就是準(zhǔn)備配置文件:

(1)、 在項(xiàng)目的values文件夾里面創(chuàng)建 attrs.xml :

復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyRadioButton">
<attr name="pic" format="reference" />
</declare-styleable>
</resources>

(2)、創(chuàng)建 styles.xml:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="radioButtonStyle">
<item name="android:button">@null</item>
<item name="android:textSize">12dip</item>
<item name="android:gravity">center_horizontal|bottom</item>
<item name="android:paddingBottom">5dip</item>
</style>

</resources>

(3)、把中文定義在string.xml里:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="hello">Hello World, MainAct!</string>
<string name="app_name">TabHost</string>
<string name="home">大廳</string>
<string name="account">用戶</string>
<string name="beanExchange">玩具</string>
<string name="winAcciche">公告</string>
<string name="more">更多</string>

</resources>

(4)、創(chuàng)建MyRadioButton類繼承RadioButton:
復(fù)制代碼 代碼如下:

package com.dome.viewer.widget;

import com.dome.viewer.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.widget.RadioButton;


public class MyRadioButton extends RadioButton {

private Drawable drawable;

public MyRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton);
drawable = a.getDrawable(R.styleable.MyRadioButton_pic);
}
//Drawable轉(zhuǎn)換成Bitmap
private Bitmap drawable2Bitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof NinePatchDrawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
} else {
return null;
}
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap image = drawable2Bitmap(drawable);
if (image != null) {
Paint pt = new Paint();
pt.setARGB(255, 66, 66, 66);
// 消除鋸齒
pt.setAntiAlias(true);
// 居中顯示圖片
int imageX = (int) (this.getWidth() - image.getWidth()) / 2;
canvas.drawBitmap(image, imageX, 2, pt);
pt.setARGB(255, 255, 255, 255);
}
}
}

(5)、為Activity準(zhǔn)備布局文件,命名為:tabhost.xml:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:attrstest="http://schemas.android.com/apk/res/com.dome.viewer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg" >

<RelativeLayout
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:background="@drawable/bg_navigation" >

<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="5dip"
android:gravity="center"
android:text="首頁"
android:textSize="25dip" />
</RelativeLayout>

<android.support.v4.view.ViewPager
android:id="@+id/vPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:paddingBottom="55dip"
android:persistentDrawingCache="animation" />

<RadioGroup
android:id="@+id/rg_main_btns"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="@drawable/bg_navigation"
android:gravity="center_horizontal"
android:orientation="horizontal" >

<com.dome.viewer.widget.MyRadioButton
android:id="@+id/buyHomeTab"
style="@style/radioButtonStyle"
android:layout_width="60dip"
android:layout_height="50dip"
android:background="@drawable/navigation_item"
android:checked="true"
attrstest:pic="@drawable/gcdt"
android:text="@string/home" />

<com.dome.viewer.widget.MyRadioButton
android:id="@+id/winAfficheTab"
style="@style/radioButtonStyle"
android:layout_width="60dip"
android:layout_height="50dip"
android:background="@drawable/navigation_item"
android:button="@null"
attrstest:pic="@drawable/kjgg"
android:text="@string/winAcciche" />

<com.dome.viewer.widget.MyRadioButton
android:id="@+id/integralTab"
style="@style/radioButtonStyle"
android:layout_width="65dip"
android:layout_height="50dip"
android:background="@drawable/navigation_item"
attrstest:pic="@drawable/jfdh"
android:text="@string/beanExchange" />

<com.dome.viewer.widget.MyRadioButton
android:id="@+id/accountTab"
style="@style/radioButtonStyle"
android:layout_width="60dip"
android:layout_height="50dip"
android:background="@drawable/navigation_item"
attrstest:pic="@drawable/yhzx"
android:text="@string/account" />

<com.dome.viewer.widget.MyRadioButton
android:id="@+id/moreTab"
style="@style/radioButtonStyle"
android:layout_width="60dip"
android:layout_height="50dip"
android:background="@drawable/navigation_item"
attrstest:pic="@drawable/more"
android:text="@string/more" />
</RadioGroup>

</RelativeLayout>

(6)、創(chuàng)建TabHostActivity:  
復(fù)制代碼 代碼如下:

package com.dome.viewer;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
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.View;
import android.view.Window;
import android.widget.RadioGroup;

public class TabHostActivity extends Activity {


@Override
protected void onStart() {
super.onStart();
}

private RadioGroup radioGroup;

// 頁卡內(nèi)容
private ViewPager mPager;
// Tab頁面列表
private List<View> listViews;
// 當(dāng)前頁卡編號
private LocalActivityManager manager = null;

private MyPagerAdapter mpAdapter = null;
private int index;

// 更新intent傳過來的值
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
}

@Override
protected void onSaveInstanceState(Bundle outState) {

}
@Override
public void onBackPressed() {
Log.i("","onBackPressed()");
super.onBackPressed();
}
@Override
protected void onPause() {
Log.i("","onPause()");
super.onPause();
}

@Override
protected void onStop() {
Log.i("","onStop()");
super.onStop();
}

@Override
protected void onDestroy() {
Log.i("","onDestroy()");
super.onDestroy();
}


@Override
protected void onResume() {
super.onResume();

if(getIntent() != null){
index = getIntent().getIntExtra("index", 0);
mPager.setCurrentItem(index);
setIntent(null);
}else{
if(index < 4){
index = index+1;
mPager.setCurrentItem(index);
index = index -1;
mPager.setCurrentItem(index);

}else if(index == 4){
index= index-1;
mPager.setCurrentItem(index);
index = index +1;
mPager.setCurrentItem(index);
}
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tabhost);
mPager = (ViewPager) findViewById(R.id.vPager);
manager = new LocalActivityManager(this, true);
manager.dispatchCreate(savedInstanceState);
InitViewPager();
radioGroup = (RadioGroup) this.findViewById(R.id.rg_main_btns);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {

case R.id.buyHomeTab:
index = 0;
listViews.set(0, getView("A", new Intent(TabHostActivity.this, OneDomeActivity.class)));
mpAdapter.notifyDataSetChanged();
mPager.setCurrentItem(0);
break;

case R.id.winAfficheTab:
index = 1;
listViews.set(1, getView("B", new Intent(TabHostActivity.this, TowDomeActivity.class)));
mpAdapter.notifyDataSetChanged();
mPager.setCurrentItem(1);
break;

case R.id.integralTab:
index = 2;
listViews.set(2, getView("C", new Intent(TabHostActivity.this, ThreeDomeActivity.class)));
mpAdapter.notifyDataSetChanged();
mPager.setCurrentItem(2);
break;

case R.id.accountTab:
index = 3;
listViews.set(3, getView("D", new Intent(TabHostActivity.this, FourDomeActivity.class)));
mpAdapter.notifyDataSetChanged();
mPager.setCurrentItem(3);
break;

case R.id.moreTab:
index = 4;
listViews.set(4, getView("E", new Intent(TabHostActivity.this, FiveDomeActivity.class)));
mpAdapter.notifyDataSetChanged();
mPager.setCurrentItem(4);
break;
default:
break;
}
}
});
}

/**
* 初始化ViewPager
*/
private void InitViewPager() {
Intent intent = null;
listViews = new ArrayList<View>();
mpAdapter = new MyPagerAdapter(listViews);
intent = new Intent(TabHostActivity.this, OneDomeActivity.class);
listViews.add(getView("A", intent));
intent = new Intent(TabHostActivity.this, TowDomeActivity.class);
listViews.add(getView("B", intent));
intent = new Intent(TabHostActivity.this, ThreeDomeActivity.class);
listViews.add(getView("C", intent));
intent = new Intent(TabHostActivity.this, FourDomeActivity.class);
listViews.add(getView("D", intent));
intent = new Intent(TabHostActivity.this, FiveDomeActivity.class);
listViews.add(getView("E", intent));
mPager.setOffscreenPageLimit(0);
mPager.setAdapter(mpAdapter);
mPager.setCurrentItem(0);
mPager.setOnPageChangeListener(new MyOnPageChangeListener());
}

/**
* ViewPager適配器
*/
public class MyPagerAdapter extends PagerAdapter {
public List<View> mListViews;

public MyPagerAdapter(List<View> mListViews) {
this.mListViews = mListViews;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView(mListViews.get(arg1));
}

@Override
public void finishUpdate(View arg0) {
}

@Override
public int getCount() {
return mListViews.size();
}

@Override
public Object instantiateItem(View arg0, int arg1) {
((ViewPager) arg0).addView(mListViews.get(arg1), 0);
return mListViews.get(arg1);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == (arg1);
}

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}

@Override
public Parcelable saveState() {
return null;
}

@Override
public void startUpdate(View arg0) {
}
}

/**
* 頁卡切換監(jiān)聽,ViewPager改變同樣改變TabHost內(nèi)容
*/
public class MyOnPageChangeListener implements OnPageChangeListener {

public void onPageSelected(int arg0) {
manager.dispatchResume();
switch (arg0) {
case 0:
index = 0;
radioGroup.check(R.id.buyHomeTab);
listViews.set(0, getView("A", new Intent(TabHostActivity.this, OneDomeActivity.class)));
mpAdapter.notifyDataSetChanged();
break;
case 1:
index = 1;
radioGroup.check(R.id.winAfficheTab);
listViews.set(1, getView("B", new Intent(TabHostActivity.this, TowDomeActivity.class)));
mpAdapter.notifyDataSetChanged();
break;
case 2:
index = 2;
radioGroup.check(R.id.integralTab);
listViews.set(2, getView("C", new Intent(TabHostActivity.this, ThreeDomeActivity.class)));
mpAdapter.notifyDataSetChanged();
break;
case 3:
index = 3;
radioGroup.check(R.id.accountTab);
listViews.set(3, getView("D", new Intent(TabHostActivity.this, FourDomeActivity.class)));
mpAdapter.notifyDataSetChanged();
break;
case 4:
index = 4;
radioGroup.check(R.id.moreTab);
listViews.set(4, getView("E", new Intent(TabHostActivity.this, FiveDomeActivity.class)));
mpAdapter.notifyDataSetChanged();
break;
}
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) {
}
}

private View getView(String id, Intent intent) {
return manager.startActivity(id, intent).getDecorView();
}

}

(7)、然后依次創(chuàng)建5個Activity作為頁卡,和創(chuàng)建5個xml作為Activity的布局文件,如圖:  

源碼下載 

相關(guān)文章

  • Kotlin 協(xié)程的異常處理準(zhǔn)則

    Kotlin 協(xié)程的異常處理準(zhǔn)則

    協(xié)程是互相協(xié)作的程序,協(xié)程是結(jié)構(gòu)化的,正是因?yàn)閰f(xié)程的這兩個特點(diǎn),導(dǎo)致它和 Java 的異常處理機(jī)制不一樣,這篇文章重點(diǎn)給大家介紹Kotlin 協(xié)程的異常處理準(zhǔn)則,感興趣的朋友一起看看吧
    2024-01-01
  • 詳解Android 裸眼3D效果View控件

    詳解Android 裸眼3D效果View控件

    主要的設(shè)計(jì)核心是依賴于傳感器對手機(jī)晃動的監(jiān)聽(重力感應(yīng)監(jiān)聽器),對每層圖片進(jìn)行不同的移動,實(shí)現(xiàn)仿3D效果。本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-08-08
  • Android實(shí)現(xiàn)截圖和分享功能的代碼

    Android實(shí)現(xiàn)截圖和分享功能的代碼

    截圖和分享功能大家都玩過,下面通過本文給大家介紹Android實(shí)現(xiàn)截圖和分享功能,代碼簡單易懂,非常不錯,具有參考借鑒價(jià)值,需要的的朋友參考下吧
    2017-07-07
  • Android斷點(diǎn)續(xù)傳下載器JarvisDownloader的示例

    Android斷點(diǎn)續(xù)傳下載器JarvisDownloader的示例

    本篇文章主要介紹了Android斷點(diǎn)續(xù)傳下載器JarvisDownloader的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • android相冊選擇圖片的編碼實(shí)現(xiàn)代碼

    android相冊選擇圖片的編碼實(shí)現(xiàn)代碼

    本篇文章主要介紹了android相冊選擇圖片的編碼實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 詳解Android開發(fā)中Activity的四種launchMode

    詳解Android開發(fā)中Activity的四種launchMode

    這篇文章主要介紹了Android開發(fā)中Activity的四種launchMode,launchMode主要用于控制多個Activity間的跳轉(zhuǎn),需要的朋友可以參考下
    2016-03-03
  • 關(guān)于RxJava的一些特殊用法小結(jié)

    關(guān)于RxJava的一些特殊用法小結(jié)

    RxJava 是一個響應(yīng)式編程框架,采用觀察者設(shè)計(jì)模式。下面這篇文章主要總結(jié)介紹了一些關(guān)于RxJava的特殊用法,需要的朋友可以參考借鑒,下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-05-05
  • Android BroadcastReceiver實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽

    Android BroadcastReceiver實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽

    這篇文章主要為大家詳細(xì)介紹了Android BroadcastReceiver實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android軟鍵盤彈出時(shí)的界面控制方法

    Android軟鍵盤彈出時(shí)的界面控制方法

    這篇文章主要介紹了Android軟鍵盤彈出時(shí)的界面控制方法,結(jié)合實(shí)例形式分析了Android軟鍵盤彈出后的三種模式,涉及Android針對AndroidManifet.xml的修改技巧,需要的朋友可以參考下
    2016-08-08
  • Android recyclerView橫條指示器實(shí)現(xiàn)淘寶菜單模塊

    Android recyclerView橫條指示器實(shí)現(xiàn)淘寶菜單模塊

    這篇文章主要為大家詳細(xì)介紹了recyclerView橫條指示器實(shí)現(xiàn)淘寶菜單模塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02

最新評論

班戈县| 花莲县| 明星| 高淳县| 贵溪市| 莱州市| 万荣县| 河东区| 五华县| 洪洞县| 榆树市| 利辛县| 浦县| 安义县| 新晃| 柳州市| 忻城县| 乌拉特前旗| 柳河县| 桓仁| 读书| 汽车| 南汇区| 徐州市| 楚雄市| 彭州市| 晋城| 大埔区| 金川县| 鄂尔多斯市| 黄大仙区| 琼结县| 丰县| 吴堡县| 周至县| 英德市| 加查县| 瑞金市| 通道| 洪泽县| 凤冈县|