使用自定义RadioButton和ViewPager实现TabHost效果和带滑动的页卡效果。

   参考自http://www.apkbus.com/android-86125-1-1.html

   这篇文章技术含量一般,大家别见笑。源码我以测试,在底部可下载。    好了先上效果图:

以下是实现步骤:       

1、准备自定义RadioButton控件的样式图片等,就是准备配置文件:
(1)、  在项目的values文件夹里面创建 attrs.xml :

 

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

 

(2)、创建 styles.xml:

?
<?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里:

 

?
<?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)、    创建MyRadioButton类继承RadioButton:

?
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转换成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准备布局文件,命名为:tabhost.xml:

?
<?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)、创建TabHostActivity:  

?
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;
        
        // 页卡内容
        private ViewPager mPager;
        // Tab页面列表
        private List<View> listViews;
        // 当前页卡编号
        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) {
                }
        }
        /**
         * 页卡切换监听,ViewPager改变同样改变TabHost内容
         */
        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)、然后依次创建5个Activity作为页卡,和创建5个xml作为Activity的布局文件,如图:  

 欢迎关注http://e.weibo.com/2975543812

源码下载:http://files.cnblogs.com/feifei1010/TabHostDome.rar 

转载于:https://www.cnblogs.com/crazywenza/archive/2013/01/23/2873362.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274374.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

利益相关者软件工程_改善开发人员团队与非技术利益相关者之间交流的方法

利益相关者软件工程Whether you’re working on a startup or a big company, keeping your stakeholders and non-technical partners engaged and up to date on what the tech team has been building can be hard.无论您是在初创公司还是大公司中工作&#xff0c;都要让您的…

响应式网格项目动画布局_响应式网格及其实际使用方式:常见的UI布局

响应式网格项目动画布局重点 (Top highlight)第二部分 (Part II) Now that you have a basic understanding of how to use grids, you might be wondering how to apply them to layouts you see on the web. Responsive grids are a method to systematically align your des…

时间轴ui设计_我应该在UI设计上花更多时间吗?

时间轴ui设计Let’s start with an example of communication skills: they are important for any profession, and you expect any professional to have a decent level. However, excellent communication skills won’t make up for the lack of core expertise. Imagine …

移动端分步注册_移动应用程序的可用性测试:分步指南

移动端分步注册Written by Justin Mifsud由贾斯汀米夫苏德 ( Justin Mifsud)撰写 The mobile market is huge and growing at a very fast rate. With an estimated 4.5 billion subscribers worldwide, it is forecasted that the number of mobile phones will surpass the …

插图 引用 同一行两个插图_提出食物主题中的插图

插图 引用 同一行两个插图I have a page in my portfolio, which is about search functionality. I wanted that page to feel fun and engaging, to convey a positive vibe, so I decided to add illustrations to it.我的投资组合中有一个页面与搜索功能有关。 我希望该页面…

脸部细微表情识别_您可以仅使用面部表情来控制字体吗?

脸部细微表情识别原型 (The prototype) Facetype is the name of Adam’s interactive project, in which the emotions detected from a person’s facial gestures control a variable font. To each detected emotion corresponds a specific typeface, which keeps transfo…

uva10891Game of sum

题意:经典的取石子游戏是这样的:有一堆石子&#xff0c;A、B两个人轮流取&#xff0c;每次取一颗&#xff0c;只能从边上取&#xff0c;每个石子有相应的价值&#xff0c;A、B两人都想使得自己的价值最多&#xff0c;两个人足够聪明&#xff0c;问最后价值分别是多少 本题则是可…

用户体验设计师能为seo做_用户体验设计师可以从产品设计历史中学到什么

用户体验设计师能为seo做Many things have changed from tool design in the prehistoric era to today’s digital product design. However, we can see surprisingly many similarities. Especially when it comes down to one particular aspect: usability.从史前时代的工…

orton效果_如何使图片发光:Orton效果

orton效果Have you ever seen an impossibly dream-like landscape photo? One with a slow burning, glowing sunset. That’s really the best way to describe it, the image looks as if it’s glowing. You might be thinking, “wow, I wish I was that good and could …

UVA10785 The Mad Numerologist

虽然是sorting的压轴&#xff0c;但是比起前面真心水题。这个专题结合前面string的很多&#xff0c;排序相对简单了&#xff0c;qsort基本解决。 题目&#xff1a; The Mad Numerologist Numerology is a science that is used by many people to find out a mans personality,…

苹果人机交互指南_苹果人机界面设计指南的10个见解

苹果人机交互指南重点 (Top highlight)I’ve been developing an IOS app for the past few months and have been constantly referring to Apple’s Human Interface Design Guidelines. I would consider it a must-read for any aspiring or current UI/UX designer.在过去…

也来学学插件式开发

上一家公司有用到插件式开发来做一个工具箱&#xff0c;类似于QQ电脑管家&#xff0c;有很多工具列表&#xff0c;点一下工具下载后就可以开始使用了。可惜在那家公司待的时候有点短&#xff0c;没有好好研究一下。现在有空&#xff0c;自己在网上找了些资料&#xff0c;也来试…

同态加法_我对同态的想法

同态加法Early February, I uploaded this shot onto Dribbble. Nothing fancy –– just two screens experimenting with “2月初&#xff0c;我将这张照片上传到Dribbble。 没什么幻想–只有两个屏幕在尝试“ Neumorphism,” or soft UI. Little did I know that this post…

php内核探索

引自&#xff1a;http://www.nowamagic.net/librarys/veda/detail/1285 SAPI:Server Application Programming Interface 服务器端应用编程端口。研究过PHP架构的同学应该知道这个东东的重要性&#xff0c;它提供了一个接口&#xff0c;使得PHP可以和其他应用进行交互数据。 本…

hp-ux锁定用户密码_UX设计101:用户研究-入门需要了解的一切

hp-ux锁定用户密码这是什么&#xff1f; (What is this?) This session is part of a learning curriculum that I designed to incrementally skill up and empower a team of Designers and Researchers whose skillset and ways of working needed to evolve to keep up wi…

extjs6 引入ux_关于UX以及如何摆脱UX的6种常见误解

extjs6 引入uxDo you ever browse social media, internet, or talk to colleagues and hear them say something UX related you disagree with so much that you just want to lecture them on the spot?您是否曾经浏览过社交媒体&#xff0c;互联网或与同事交谈&#xff0c…

Cocos2D-HTML5开源2D游戏引擎

http://www.programmer.com.cn/12198/ Cocos2D-HTML5是基于HTML5规范集的Cocos2D引擎的分支&#xff0c;于2012年5月发布。Cocos2D-HTML5的作者林顺将在本文中介绍Cocos2D-HTML5的框架、API、跨平台能力以及强大的性能。Cocos2D-HTML5是Cocos2D系列引擎随着互联网技术演进而产生…

illustrator下载_Illustrator笔工具练习

illustrator下载Adobe Illustrator is a fantastic vector creation tool and you can create a lot of things without ever using the Pen Tool. However, if you want to use Illustrator at its full potential, I personally believe that you need to master and become …

怎么更好练习数位板_如何设计更好的仪表板

怎么更好练习数位板重点 (Top highlight)Dashboard noun \ˈdash-ˌbȯrd\ A screen on the front of a usually horse-drawn vehicle to intercept water, mud, or snow.仪表盘 名词\ ˈdash-ˌbȯrd \\通常在马拉的车辆前部的屏幕&#xff0c;用来拦截水&#xff0c;泥或雪。…

学习正则表达式

deerchao的blog Be and aware of who you are. 正则表达式30分钟入门教程 来园子之前写的一篇正则表达式教程&#xff0c;部分翻译自codeproject的The 30 Minute Regex Tutorial。 由于评论里有过长的URL,所以本页排版比较混乱,推荐你到原处查看,看完了如果有问题,再到这里来提…