`
pxq19890719
  • 浏览: 51770 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

新浪微博UI设计学习之妙用TabHost

阅读更多
声明

  欢迎转载,但请保留文章原始出处:)

    博客园:http://www.cnblogs.com

    农民伯伯: http://www.cnblogs.com/over140



版本

  新浪微博 weibo_10235010.apk



正文

  一、效果图


    

    红色部分是本文要实现的目标。



  二、实现

    maintabs.xml
<?xml version="1.0" encoding="UTF-8"?>
<TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
        <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" />
        <TabWidget android:id="@android:id/tabs" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" />
        <RadioGroup android:gravity="center_vertical" android:layout_gravity="bottom" android:orientation="horizontal" android:id="@id/main_radio" android:background="@drawable/maintab_toolbar_bg" android:layout_width="fill_parent" android:layout_height="wrap_content">
            <RadioButton   android:text="@string/main_home" android:checked="true" android:id="@+id/radio_button0" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_1_n" style="@style/main_tab_bottom" />
            <RadioButton android:id="@+id/radio_button1" android:layout_marginTop="2.0dip" android:text="@string/main_news" android:drawableTop="@drawable/icon_2_n" style="@style/main_tab_bottom" />
            <RadioButton android:id="@+id/radio_button2" android:layout_marginTop="2.0dip" android:text="@string/main_my_info" android:drawableTop="@drawable/icon_3_n" style="@style/main_tab_bottom" />
            <RadioButton android:id="@+id/radio_button3" android:layout_marginTop="2.0dip" android:text="@string/menu_search" android:drawableTop="@drawable/icon_4_n" style="@style/main_tab_bottom" />
            <RadioButton android:id="@+id/radio_button4" android:layout_marginTop="2.0dip" android:text="@string/more" android:drawableTop="@drawable/icon_5_n" style="@style/main_tab_bottom" />
        </RadioGroup>
    </LinearLayout>
</TabHost>

styles.xml
    <style name="main_tab_bottom">
        <item name="android:textSize">@dimen/bottom_tab_font_size</item>
        <item name="android:textColor">#ffffffff</item>
        <item name="android:ellipsize">marquee</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:background">@drawable/home_btn_bg</item>
        <item name="android:paddingTop">@dimen/bottom_tab_padding_up</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:button">@null</item>
        <item name="android:singleLine">true</item>
        <item name="android:drawablePadding">@dimen/bottom_tab_padding_drawable</item>
        <item name="android:layout_weight">1.0</item>
    </style>

home_btn_bg.xml
        <selector
          xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:state_focused="true" android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/home_btn_bg_s" />
            <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/home_btn_bg_s" />
            <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/home_btn_bg_d" />
            <item android:drawable="@drawable/transparent" />
        </selector>

代码说明:

        1.  需要注意的是他这里把TabWidget的Visibility设置成了gone!也就是默认难看的风格不见了:,取而代之的是5个带风格的单选按钮.

        2.  注意为单选按钮设置的style,其中最重要的是为其background设置了home_btn_bg.xml,也就是自定义了选中效果。

    Java文件
public class MainTabActivity extends TabActivity implements
        OnCheckedChangeListener {

    private TabHost mHost;
    private Intent mMBlogIntent;
    private Intent mMoreIntent;
    private Intent mInfoIntent;
    private Intent mSearchIntent;
    private Intent mUserInfoIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.maintabs);

        // ~~~~~~~~~~~~ 初始化
        this.mMBlogIntent = new Intent(this, HomeListActivity.class);
        this.mSearchIntent = new Intent(this, SearchSquareActivity.class);
        this.mInfoIntent = new Intent(this, MessageGroup.class);
        this.mUserInfoIntent = new Intent(this, MyInfoActivity.class);
        this.mMoreIntent = new Intent(this, MoreItemsActivity.class);

        initRadios();
        
        setupIntent();
    }

    /**
     * 初始化底部按钮
     */
    private void initRadios() {
         ((RadioButton) findViewById(R.id.radio_button0)).setOnCheckedChangeListener(this);
         ((RadioButton) findViewById(R.id.radio_button1)).setOnCheckedChangeListener(this);
         ((RadioButton) findViewById(R.id.radio_button2)).setOnCheckedChangeListener(this);
         ((RadioButton) findViewById(R.id.radio_button3)).setOnCheckedChangeListener(this);
         ((RadioButton) findViewById(R.id.radio_button4)).setOnCheckedChangeListener(this);
    }

    /**
     * 切换模块
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            switch (buttonView.getId()) {
            case R.id.radio_button0:
                this.mHost.setCurrentTabByTag("mblog_tab");
                break;
            case R.id.radio_button1:
                this.mHost.setCurrentTabByTag("message_tab");
                break;
            case R.id.radio_button2:
                this.mHost.setCurrentTabByTag("userinfo_tab");
                break;
            case R.id.radio_button3:
                this.mHost.setCurrentTabByTag("search_tab");
                break;
            case R.id.radio_button4:
                this.mHost.setCurrentTabByTag("more_tab");
                break;
            }
        }
    }

    private void setupIntent() {
        this.mHost = getTabHost();
        TabHost localTabHost = this.mHost;

        localTabHost.addTab(buildTabSpec("mblog_tab", R.string.main_home,
                R.drawable.icon_1_n, this.mMBlogIntent));

        localTabHost.addTab(buildTabSpec("message_tab", R.string.main_news,
                R.drawable.icon_2_n, this.mInfoIntent));

        localTabHost.addTab(buildTabSpec("userinfo_tab", R.string.main_my_info,
                R.drawable.icon_3_n, this.mUserInfoIntent));

        localTabHost.addTab(buildTabSpec("search_tab", R.string.menu_search,
                R.drawable.icon_4_n, this.mSearchIntent));

        localTabHost.addTab(buildTabSpec("more_tab", R.string.more,
                R.drawable.icon_5_n, this.mMoreIntent));

    }

    private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
            final Intent content) {
        return this.mHost
                .newTabSpec(tag)
                .setIndicator(getString(resLabel),
                        getResources().getDrawable(resIcon))
                .setContent(content);
    }

代码说明

      1.  由于TabWidget被隐藏,所以相关的事件也会无效,这里取巧用RadioGroup与RadioButton的特性来处理切换,然后监听事件调用setCurrentTabByTag来切换Activity。

      2.  注意即使TabWidget被隐藏,也要为其设置indicator,否则会保持。
分享到:
评论

相关推荐

    仿新浪微博TabHost菜单

    在Android应用开发中,"仿新浪微博TabHost菜单"是一个常见的设计模式,用于实现类似新浪微博那样具有多标签切换功能的界面。...通过这个特效集锦系列的学习,你可以进一步提升自己的Android UI设计和编程技能。

    仿新浪微博 的Tabhost

    总的来说,"仿新浪微博的Tabhost"项目涵盖了Android UI设计、组件定制、事件监听以及数据处理等多个知识点,对开发者来说是一个很好的实践案例,可以帮助他们深入理解和掌握Android应用开发的各个方面。通过这样的...

    (框架)Android例子源码——tabhost+activity+slidingMenu仿新浪微博布局.zip

    该压缩包文件是一个Android开发的学习资源,主要展示了如何使用TabHost、Activity以及SlidingMenu来构建一个类似新浪微博的用户界面。下面将详细解释这些技术及其在Android应用开发中的作用。 **TabHost**: TabHost...

    tabhost activity slidingMenu仿新浪微博布局.zip

    7. **学习和实践**: 对于初学者,这是一个很好的学习资源,可以帮助理解Android UI设计的基本原理,特别是如何组合使用不同的组件来实现复杂的功能。同时,对有经验的开发者来说,这也可以作为参考,启发他们设计...

    新浪微博源码

    【新浪微博源码】是一个关于构建类似新浪微博应用的代码实现,其中包含了多个关键技术和组件的运用。...通过深入研究这个源码,可以提升对Android UI设计、事件处理、数据验证和文本格式化等关键技能的理解和实践能力。

    android activity选项卡 listview 下拉刷新 仿新浪微博

    4. **新浪微博UI设计**: - 模仿新浪微博的UI设计,意味着要实现类似的信息流布局,包括文字、图片、链接等混合展示,以及评论、点赞、分享等互动功能。 - 通常需要自定义复杂的ListView Item布局,同时考虑不同...

    Android例子源码tabhost+activity+slidingMenu仿新浪微博布局.zip

    本示例源码"Android例子源码tabhost+activity+slidingMenu仿新浪微博布局.zip"提供了一个具体的实践,模仿了流行的社交媒体应用——新浪微博的布局设计。下面我们将深入探讨这些关键组件以及它们如何协同工作。 **...

    Fragment+FragmentTabHost实现仿新浪微博底部菜单栏效果(源码)

    在Android应用开发中,设计一个类似新浪微博的底部菜单栏是常见的需求,这涉及到对Fragment和FragmentTabHost的熟练运用。本文将深入解析如何利用这两个组件来实现这一功能,并提供源码示例。 Fragment是Android ...

    新浪微博tab菜单

    在Android应用开发中,"新浪微博tab菜单"是一个常见的界面元素,用于展示多个功能模块并方便用户切换。在标题和描述中提到的实现方法是通过自定义tab,隐藏原生的`TabWidget`,转而使用`RadioGroup`来替代。这种方法...

    FragmentTabHost 实现新浪微博Tab效果

    在Android应用开发中,创建一个类似新浪微博的Tab切换界面是一项常见的需求。`FragmentTabHost`是Android提供的一个组件,用于在Tab布局中管理和显示`Fragment`。本教程将详细介绍如何利用`FragmentTabHost`和`...

    android自定义tabhost

    总的来说,自定义TabHost是一个涉及多个技术点的过程,包括UI设计、事件监听、数据处理和性能优化等。通过巧妙地组合这些技术,我们可以创建出功能强大且用户友好的多标签界面,就像在新浪微博中看到的那样。

    fragment+fragmenttabHost仿新浪微博底部菜单栏

    在Android应用开发中,"fragment+fragmenttabHost仿新浪微博底部菜单栏"是一个常见的设计模式,用于构建具有多个可切换视图的应用界面,类似于微博应用中的底部导航栏。这个设计允许用户在不同功能区域之间平滑地...

    Android之底部導航欄--RadioGroup、TabHost、Fragment

    本项目旨在模仿新浪微博的底部导航栏,通过结合使用`RadioGroup`、`TabHost`和`Fragment`来实现这一功能。下面将详细介绍这三个关键组件及其在实现过程中扮演的角色。 首先,`RadioGroup`是Android中的一个视图组...

    Android仿新浪微博分页管理界面(3)

    在Android应用开发中,创建一个类似新浪微博的分页管理界面是一项常见的需求,这涉及到用户界面的布局设计、页面间的导航以及数据的加载和刷新。本示例中,开发者通过继承`TabActivity`来实现多页面的管理,利用`...

    AiUi.rar_home_btn_bg.png

    总的来说,这个标题和描述涉及的知识点包括:UI设计、TabHost组件、Android开发、固定导航、社交媒体应用设计参考以及图像资源管理。在实际开发或设计过程中,理解并合理运用这些知识点可以提升产品的可用性和美观度...

    引导页+底部菜单

    5. 为了达到仿新浪微博的效果,可以自定义`TabHost`的样式,包括字体、颜色、图标等,以实现与主题一致的视觉效果。 6. 考虑到状态栏和底部菜单的适配,可能需要使用`android:fitsSystemWindows="true"`属性或者在...

    android tab现成框架

    3. **第三方库**: 在这个"android tab现成框架"中提到的SinaWeibo可能是指某个开源项目,用于快速实现类似新浪微博的Tab功能。这类库通常已经封装好了Tab和ViewPager的交互,只需要少量代码就可以快速集成。例如,`...

Global site tag (gtag.js) - Google Analytics