深入浅出android/ophone UI实现水平布局的Tab控件

QTabWidget的QTabBar位于左侧且横向 如何使QTabWidget的QTabBar位于左侧且横向? 阅读详情

深入浅出android/ophone UI实现水平布局的Tab控件

作者:anychen

Tab UI控件是非常常见、常用的导航UI组件,使用它能够很方便地迅速切换页面。甚为强大的android当然也少不了它,在android/ophone平台上也可以很方便地使用TabHost,TabWidget来实现你的Tab。有点遗憾的是目前系统自带的UI布局只能实现垂直方向的Tab UI, 在某些项目我们不免需要水平方向的Tab UI,如果你有这种需求你可以参考本文所介绍方法。本文所述方法参考API 源代码内部实现。
1预备知识:TabHost,TabWidget, TabSpec简介
TabHost:是整个Tab ui的布局容器实质上就是是一个扩展的FrameLayout,所有的Tab UI相关组件必须位于该布局里,必须包含的组件有:
      名为TabWidget的(选项卡组件布局)和选项卡内容容器
TabWidget:选项卡组件布局容器,实质上一个线性布局LinearLayout,所有Tab选项控件都放在这里,你可以使用tabHost.addTab(TabSpec tab       Spec)方法将不同的选项卡内容添加的选项卡布局容器中,或许你会有点迷糊这明明是TabSpec嘛,不要急接着看
TabSpec:是一个与选项卡相关描述文件,其内部包含了一个类型为View[更确切地说是RelativeLayout]的tabIndicator,这个tabIndicator就是选项卡对应的视图,当你在执行t         abHost.addTab(TabSpec tabSpec)时,内部会有个操作将此tabIndicator添加到TabWidget中:下面是摘自API的TabHost.java中关于addTab的一段代码
         public void addTab(TabSpec tabSpec) {
          //...
           mTabWidget.addView(tabIndicator);
           mTabSpecs.add(tabSpec);
             //...
        }
外加一个固定ID为 android:id="@android:id/tabcontent"的FrameLayout,用于容纳Tab选项卡对应的内容
2正题:一步一步编写水平方向的Tab:
第一步:改写你的tab_indicator:直接从SDK..\platforms\android-ndata\res\layout中:拷贝tab_indicator.xml命名为tab_indicator_horizontal.xml 存放于工程中layout目录中,并修改如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="64dip"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:layout_marginTop="0dip"
    android:layout_marginBottom="0dip"
    android:orientation="vertical"
    android:background="@drawable/tab_indicator_horizontal">

    <ImageView android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
    />
    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        style="?android:attr/tabWidgetStyle"
    />
</RelativeLayout>

第二步:编写水平Tab选项卡显示状态背景:android:background="@drawable/tab_indicator_horizontal"
 同样直接从SDK..\platforms\android-ndata\res\drawable中直接拷贝tab_indicator.xml并命名为tab_indicator_horizontal.xml 存放于工程中drawable目录中,内容如下,很简单相信你应该明白是怎么回事,这个文件不需要改动,拿来就用:
 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" />
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" />

    <!-- Pressed -->
    <item android:state_pressed="true" android:drawable="@drawable/tab_press" />
</selector>
 说明:上面的drawable/tab_unselected,drawable/tab_selected实质就是图片,android本身提供的名为9patch的可拉伸自适应填充图,因为系统本身自带的是垂直方向的图,不适合在这里使用,你需要自己修改上述图片,为了简要说明这里直接使自定义图形来适配:
第三步:使用自定义图形来适配选项卡显示状态背景:
tab_focus.xml,其他如tab_unselected你亦可参照此格式自定义,这里不再重复简述
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#88FF88" android:endColor="#55EE55"
            android:angle="0"/>
    <corners android:radius="6dp" />
</shape>

第四步:编写Tab ui布局main_tab_horizontal.xml:
注意下面图片使用的的定义,请自行修改
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:background="@drawable/style_theme"
 android:layout_width="fill_parent" android:layout_height="fill_parent">
 <TabHost android:id="@+id/tabHost" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:layout_weight="1">
  <!-- 水平方向的Tab -->
  <LinearLayout android:orientation="horizontal"
   android:layout_width="fill_parent" android:layout_height="fill_parent">
   <!-- Tab标签控件 注意ID的方式-->
   <TabWidget android:id="@android:id/tabs"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:layout_weight="0" />
    
   <!-- Tab 内容控件 注意ID的方式-->
   <FrameLayout android:id="@android:id/tabcontent"
    android:layout_height="fill_parent" android:layout_width="0dip"
    android:layout_weight="1">

    <!-- 使用垂直与水平ScrollView -->
    <HorizontalScrollView android:layout_width="wrap_content"
     android:layout_height="fill_parent" android:fillViewport="true">
     <LinearLayout android:id="@+id/id_tab_view1" android:orientation="horizontal"
      android:layout_width="fill_parent" android:layout_height="fill_parent">
      
      <ImageView android:layout_width="wrap_content"
       android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical"
       android:src="@drawable/tree2" />
     </LinearLayout>
    </HorizontalScrollView>

    <HorizontalScrollView android:layout_width="wrap_content"
     android:layout_height="fill_parent" android:fillViewport="true">
     <LinearLayout android:id="@+id/id_tab_view2" android:orientation="horizontal"
      android:layout_width="fill_parent" android:layout_height="fill_parent">
      
      <ImageView android:layout_width="wrap_content"
       android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical"
       android:src="@drawable/building3" />
     </LinearLayout>
    </HorizontalScrollView>
    <HorizontalScrollView android:id="@+id/id_tab_view3" android:layout_width="wrap_content"
     android:layout_height="fill_parent" android:fillViewport="true">
     <LinearLayout android:orientation="horizontal"
      android:layout_width="fill_parent" android:layout_height="fill_parent">
      
      <ImageView android:layout_width="wrap_content"
       android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical"
       android:src="@drawable/building1" />
     </LinearLayout>
    </HorizontalScrollView>
    <HorizontalScrollView android:id="@+id/id_tab_view4" android:layout_width="wrap_content"
     android:layout_height="fill_parent" android:fillViewport="true">
     <LinearLayout android:orientation="horizontal"
      android:layout_width="fill_parent" android:layout_height="fill_parent">
      
      <ImageView android:layout_width="wrap_content"
       android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical"
       android:src="@drawable/building2" />
     </LinearLayout>
    </HorizontalScrollView>
   </FrameLayout>
  </LinearLayout>
 </TabHost>
</LinearLayout>

第五步:编写测试代码:
 public class TestActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main_tab_horizontal);
  parseHorizontalTab();
 }

 Drawable icon_tab_1, icon_tab_2, icon_tab_3, icon_tab_4;

 private void parseHorizontalTab() {
  // 注意下面的代码用的是android.R.id.tabhost,在布局中有2个ID参数是固定的需要使用固定的ID:
 
  // 选项卡:TabWidget->android:id/tabs
  // 选项内容:FrameLayout android:id="android:id/tabcontent"
  final TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
  tabHost.setup();
  icon_tab_1 = this.getResources().getDrawable(R.drawable.icon1);
  icon_tab_2 = this.getResources().getDrawable(R.drawable.icon2);
  icon_tab_3 = this.getResources().getDrawable(R.drawable.icon3);
  icon_tab_4 = this.getResources().getDrawable(R.drawable.icon4);
  createHorizontalTab(tabHost);
 }

 private void createHorizontalTab(TabHost tabHost) {
  tabHost.addTab(tabHost
    .newTabSpec("tab1")
    .setIndicator(
      createIndicatorView(this, tabHost, icon_tab_1, "tab1"))
    .setContent(R.id.id_tab_view1));
  tabHost.addTab(tabHost
    .newTabSpec("tab2")
    .setIndicator(
      createIndicatorView(this, tabHost, icon_tab_2, "tab2"))
    .setContent(R.id.id_tab_view2));

  tabHost.addTab(tabHost
    .newTabSpec("tab3")
    .setIndicator(
      createIndicatorView(this, tabHost, icon_tab_3, "tab3"))
    .setContent(R.id.id_tab_view3));
  tabHost.addTab(tabHost
    .newTabSpec("tab4")
    .setIndicator(
      createIndicatorView(this, tabHost, icon_tab_4, "tab4"))
    .setContent(R.id.id_tab_view4));

  TabWidget tw = tabHost.getTabWidget();
  tw.setOrientation(LinearLayout.VERTICAL);//注意在此处设置此参数 使TAB 垂直布局
 }
 
 /**
  * 创建自定义的 选项卡视图
  *
  * @param context
  * @param tabHost
  * @param icon
  * @return
  */
 private View createIndicatorView(Context context, TabHost tabHost,
   Drawable icon, String title) {

  LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  View tabIndicator = inflater.inflate(R.layout.tab_indicator_horizontal,
    tabHost.getTabWidget(), false);

  final ImageView iconView = (ImageView) tabIndicator
    .findViewById(R.id.icon);
  final TextView titleView = (TextView) tabIndicator
    .findViewById(R.id.title);
  titleView.setText(title);
  iconView.setImageDrawable(icon);
  return tabIndicator;
 }
 
}

结束:运行效果:

原文出处: http://www.ophonesdn.com/blog/yeya/showBlog/publicshow/050933dd-30bc-4b9f-8cc6-0c2e20ab7eb3?count=&amp;max=&amp;index=
Android垂直tab导航栏、左侧竖直tab导航栏 左侧竖向选项卡TAB导航烂,支持viewpager,不依赖viewpager,支持自定义tab样式,自带切换动画 立即下载

相关推荐

保姆级避坑指南:在NVMe SSD上安装Ubuntu 24.04,搞定RST错误和分区方案

本文提供在NVMe SSD上安装Ubuntu 24.04的详细指南,解决RST错误和分区方案问题。从BIOS设置到分区策略优化,帮助用户充分发挥NVMe固态硬盘性能,避免常见安装陷阱,实现无痛安装和后期调优。

weixin_26976635的博客 563

QTabWidegt靠左样式

QTabWidget美化的思路。

tangbolibao的博客 855

CP2102 USB 转 UART 模块 PCB 设计项目

不幸的是,我的笔记本电脑只有 USB 端口,所以我需要在 USB 端口和 UART 接口之间进行转换。PCB 布局将足够小,因此我们不一定需要高速布线 USB 线路,但我们将在此将其作为一个简单的示例来实现,以展示如何在这种类型的电路板上实现它。相反,我们只使用屏幕左侧的电路。在这里,我们利用对之间的间距和到 L1 上的铜灌注的距离将差分阻抗设置为目标值。初始布局分为两个部分:USB 连接器和 USB 线路的 TVS 二极管放置在电路板左侧,带有二极管 D4 和 D5 的引脚接头放置在电路板右侧。

jkh920184196的博客 2402

深入浅出android/ophone UI实现水平布局的Tab控件

Tab UI控件是非常常见、常用的导航UI组件,使用它能够很方便地迅速切换页面。甚为强大的android当然也少不了它,在android/ophone平台上也可以很方便地使用TabHost,TabWidget来实现你的Tab。有点遗憾的是目前系统自带的UI布局

modiaGame的专栏 1838

PCIE6.0协议学习小记-Flit_Mode

PCIE6.0学习

code_aJack的博客 4646

用LTspice仿真Buck电路输出电容:从参数计算到波形优化的全流程演示

本文详细介绍了使用LTspice仿真Buck电路输出电容的全流程,从理论计算到波形优化。通过解析关键参数如纹波电压、瞬态响应和ESR,结合工程计算方法和LTspice仿真技巧,帮助工程师优化Buck电路设计,解决输出纹波过大和瞬态响应不佳的问题。

night的博客 852

TabWidget中tabBar文字从左向右显示

在Qt中,当我们使用tabWidget,有时会把tabPosition属性设置成东西朝向的。但这时,我们会发现tabBar中的文字朝向也变了,如下图所示: 而我们总是希望文字是正对着我们的,而不是翻转过后的样子。 这里,我们可以通过setStyle来设置widget的风格,而QTabBar是继承自widget的也可以通过同样的方式,设置风格。 setStyle函数的定义为void ...

鄙人张钊_有何贵干的博客 3188

GSM的“时间管理大师”:TDMA时隙、突发脉冲与逻辑信道映射实战图解

本文深入解析GSM网络中的TDMA时隙调度与逻辑信道映射技术,揭示2G网络如何通过精密的时间管理和突发脉冲设计实现高效通信。从TDMA帧结构到GMSK调制优化,详细介绍了GSM的工程艺术及其在现代通信中的延续价值。

weixin_30631587的博客 514

自定义Android系统Tab样式

res/drawable/tab_indicator.xml res/color/tab_indicator_text.xml res/layout/tab_indicator.xml <RelativeLayout xmlns:android="http://schemas.android.

Dream It Possible 4224

微信删除的聊天记录怎么恢复(2024年最新攻略合集)

随着微信对我们生活中方方面面的渗透,微信已然从一款简单的社交软件过渡成为现时代的一种生活方式,而微信中的聊天记录就是我们和朋友之间历程的最好证明。对此,今天我们来聊聊微信删除的聊天记录怎么恢复的问题。

m0_70960708的博客 947

高通MBIM拨号流程简述

高通平台MBIM流程简述

不秃头的程序猿Gabriel的博客 5071

深入浅出android/ophone UI实现水平布局的Tab控件 Tabwidget在左边/右边

From:http://www.ophonesdn.com/blog/mobgame/showBlog/publicshow/050933dd-30bc-4b9f-8cc6-0c2e20ab7eb3;jsessionid=715C7625130217C251FBCE3C259174C8?count=&max=&index=    Tab UI控件是非常常见、常用的导航UI组件,使用它能够很方便

chuxing的专栏 5833

从零开始讲DDR(5)——读懂Datasheet

对于开发人员来说,需要根据实际场景和使用的需要,使用不同厂家,不同型号的DDR,虽然原理上大同小异,但是还是有一些细节上的需要注意的地方,接触一个新的DDR芯片,首先就是需要找到对应的datasheet,而能都读懂datasheet,也是一个硬件工程师的必备技能。

apple_53311083的博客 5224

Android垂直tab导航栏、左侧竖直tab导航栏,支持viewpager,不依赖viewpager

人生导言: 做你害怕做的事情,然后你发现,不过如此。 这是基于有时候需要左侧竖直tab导航栏 ,可以直接拿来用,继承自ScrollView 先来看下效果 部分代码: 适配器 private class MyTabAdapter implements TabAdapter { List&lt;String&gt; menus; ...

fictionss的博客 9953

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Qt QTabWidget标签页左侧显示文字无法水平显示解决方案

重写QProxyStyle #pragma once #include <QProxyStyle> #include <QStyleOptionTab> #include "CreateSteeing.h" class QSteeing : public QProxyStyle { Q_OBJECT public: QSteeing(); ~QSteeing(); //初始化 void Init(); /* sizeFromContents * 用于设.

weixin_47729470的博客 3838

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

QTabWidget 标签页tabBar放左侧,文字方向改为水平

需要重载QTabBar的paintevent,代码如下: 转自:https://forum.qt.io/topic/92923/qtabbar-paintevent-issue/4 #include <QApplication> #include <QStyleOptionTab> #include <QStylePainter> #include &l...

capooner的专栏 1万+

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

【Android基础篇】TabWidget设置背景和字体

在使用TabHost实现底部导航栏时,底部导航栏的三个导航按钮无法在布局文件里进行定制,比如设置点击时的颜色、字体的大小及颜色等,这里提供了一个解决办法,就是在代码里进行定制。思路是在Activity里给TabHost添加了分页后,在给导航栏TabWidget的导航按钮逐个添加特效(必须先添加分页,然后才能定制按钮,添加了一个分页,才会生成一个按钮)。下面是布局文件activity_main.xml

zgljl2012的专栏 4229

Qtabwidget 设置tabbar左边显示、tabbar中显示照片

Qt QTabwidget左边显示qtabbar 并且现实照片

qq_29266385的博客 837

使用Qss设置窗体样式

本文主要是对官方文档的翻译。 一、Qss作用与规则 Qss(Qtstyle sheet)是一种样式文本规范,可以使用QApplication::setStyleSheet()来设置整个应用的样式或者使用QWidget::setStyleSheet()设置某个QWidget(包括其子类)窗体的样式。Qss具有级联属性,即假设在不同的级别设置了多个样式表,那么Qt将从所有设置的样式表中派生出有效的样式表。样式表可以让您执行所有类型的定制,这些定制仅使用QPalette是很难或不可能执行的。例如,你想要黄色.

yq327115059的博客 2508

PyQt5高级界面控件之QTabWidget(六)

QTabWidget 前言 QTabWidget控件提供了一个选项卡和一个页面区域,默认显示第一个选项卡的页面,通过单击各选项卡可以查看对应的界面,如果在一个窗口中显示的输入字段很多,则可以对这些字段进行拆分,分别放置在不同界面的选项卡中 QTabWidget类中常用的方法 方法 描述 addTab() 将一个控件添加到Tab控件的选项卡中 insertTab() 将一个Tab控件的选项卡插入到指定的位置 removeTab() 根据指定的索引删除Tab控件 setCurre

李肖遥的专栏 1297

android 自定义tabwidget,自定义TabHost,TabWidget样式 .

大家好,今天我为大家分享TabHost中怎样修改TabWidget样式。在很多界面美观的应用中很多都用到了TabHost,但他们要比系统默认的要漂亮得多。先看几张图:京东商城底部菜单栏新浪微博底部菜单栏好了,看到这些漂亮的菜单栏是不是很惊讶,你可能会说用Button就可以实现啊 ,可是用Button的话控制显示的内容很麻烦,不如用TabHost控制效率更高。很想知道用TabHost是怎么实现的吧,...

weixin_32220917的博客 576

Android TabWidget设计模板

xxxxxxxxActivity .java: package com.fp.app.CmccWireless; import android.app.Activity; import android.app.TabActivity; import android.content.Intent; import android.content.res.Resources;...

helpbs 115

QTabWidget tabBar放在左侧,文字按水平方向显示

重载QTabBar的paintevent class TabBar: public QTabBar { public: QSize tabSizeHint(int index) const{ QSize s = QTabBar::tabSizeHint(index); s.transpose(); return s; } pro...

-却道天凉好个秋的博客 1万+

QTabWidget 改变tabBar位置 并改变文字方向

修改QTabWidget tabBar位置并改变文字方向

skyztttt的专栏 4万+
上一篇: android 之 TabHost
下一篇: Android的TextView使用Html来处理图片显示、字体样式、超链接等
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值