zoukankan      html  css  js  c++  java
  • android 2.2 系统中自定义title 3.0的效果

      在android中使用tab时候有很多的办法,activityGroup ,RadioGraoup,....在使用系统自带的tab的时候越到一个问题,就是tab页的大小不能控制,经过一番研究发现其实很简单,主要是定义他自己的tab页的布局,具体的实现如下:

    效果图:

    TestTab.java >>>>>>>>>>>
    /**
    * 自定义tab的使用 * TODO 简要说明该类所实现的功能 * @author pengqinping * */ public class TestTab extends TabActivity implements TabContentFactory,OnTabChangeListener { TabHost mTabHost; TabSpec tabSpec1,tabSpec2; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //由于是继承了TabActivity类。所以可以直接获取tabHost mTabHost = getTabHost(); //加载主布局 getLayoutInflater().inflate(R.layout.activity_tab_test, mTabHost.getTabContentView(), true); tv = (TextView) findViewById(R.id.tv_tab); //第一个 tab 的布局文件 LinearLayout linearLayout = (LinearLayout)getLayoutInflater().inflate(R.layout.tab_custom, null); ((TextView)linearLayout.findViewById(R.id.tv_content)).setText("tab1列表"); tabSpec1 = mTabHost.newTabSpec("Tab1").setIndicator(linearLayout).setContent(this); //第二个 tab 的布局文件 LinearLayout linearLayout1 = (LinearLayout)getLayoutInflater().inflate(R.layout.tab_custom, null); ((TextView)linearLayout1.findViewById(R.id.tv_content)).setText("tab2列表"); tabSpec2 = mTabHost.newTabSpec("Tab2").setIndicator(linearLayout1).setContent(this); mTabHost.addTab(tabSpec1); mTabHost.addTab(tabSpec2); mTabHost.setOnTabChangedListener(this); } @Override public View createTabContent(String tag) { if(tag.equals("Tab1")){ tv.setText("tab1_content_msg"); return tv; }else if(tag.equals("Tab2")){ tv.setText("tab2_content_msg"); return tv; } return null; } @Override public void onTabChanged(String tabId) { if(tabId.equals("Tab1")){ tv.setText("tab1_content_msg"); }else if(tabId.equals("Tab2")){ tv.setText("tab2_content_msg"); } }

    然后是这个activity的布局文件,注意FrameLayout的使用,

     要是没有使用的话是没有自定义的效果的,

    activity_tab_test.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" >
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <TextView
                android:id="@+id/tv_tab"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="tab" />
        </FrameLayout>
    
    </LinearLayout>

    自定义的title布局:tab_custom.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="50dip"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/tv_content"
            android:layout_width="fill_parent"
            android:layout_height="30dip"
            android:gravity="center_vertical|center_horizontal"
            android:background="@drawable/tab_select"/>
    
        <ImageView
            android:id="@+id/image_view"
            android:layout_width="fill_parent"
            android:layout_height="8dip"
            android:background="@drawable/overscroll_edge"
            android:scaleType="fitXY" />
    
    </LinearLayout>

    背景选择器的文件,

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/list_selector_background_pressed" android:state_selected="true"/>
        <item android:drawable="@drawable/list_selector_background_disabled" android:state_selected="false"/>
    
    </selector>

    图片文件如下:

     
  • 相关阅读:
    MacOS下免密码ssh登陆
    七种网卡绑定模式详解
    dnsmasq一次成功的配置
    ceph存储引擎bluestore解析
    关于并发你真的了解吗?
    Web大规模高并发请求和抢购的解决方案
    centos6.6配置rsync+sersync实现实时同步分布式多客户端分发同步
    TCP慢启动,拥塞控制,ECN 笔记
    MFS文件系统的组成
    CentOS 6.5安装部署Zabbix监控系统
  • 原文地址:https://www.cnblogs.com/pengqinping/p/2754270.html
Copyright © 2011-2022 走看看