android tabHost选项卡自定义

来源:互联网 发布:ssh框架项目源码下载 编辑:程序博客网 时间:2024/04/28 09:26



main.xml

TabHost id要自定义,不能用  android:id="@android:id/tabhost" 

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/tabho"    android:layout_width="match_parent"    android:layout_height="match_parent"     >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="wrap_content" >            <TextView                android:layout_width="fill_parent"                android:layout_height="50dip"                android:background="#ffCCCCCC"                android:gravity="center"                android:text="选项卡"                android:textColor="@color/text_color"                android:textSize="20sp" />            <Button                android:id="@+id/add_chat"                android:layout_width="45dip"                android:layout_height="45dip"                android:layout_alignParentRight="true"                android:background="@drawable/add_chat"                android:gravity="center" />        </RelativeLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical" >            <TabWidget                android:id="@android:id/tabs"                android:layout_width="fill_parent"                android:layout_height="wrap_content" >            </TabWidget>            <FrameLayout                android:id="@android:id/tabcontent"                android:layout_width="fill_parent"                android:layout_height="0dip"                android:layout_weight="1" >            </FrameLayout>        </LinearLayout>    </LinearLayout></TabHost>

标签自定义布局

tab.xml

<?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="fill_parent"      android:layout_height="wrap_content"      >              <TextView android:id="@+id/tab_label"            android:layout_width="fill_parent"          android:layout_height="50.0dip"          android:gravity="center"          android:textColor="#ffffff"        android:background="@drawable/tabbj"        android:textSize="26sp"/>   </RelativeLayout>  

在drawable文件夹下创建tabbj.xml 用来切换标签选中、不选中背景

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- 背景为图片 state_selected为true是选中,add_managebg_down、add_managebg是图片放在drawable文件夹下-->    <!--    <item android:drawable="@drawable/add_managebg_down" android:state_selected="true"/>    <item android:drawable="@drawable/add_managebg" android:state_selected="false"/>    -->    <!--背景为颜色 state_pressed为true是选中 -->    <item android:drawable="@drawable/clr_pressed" android:state_selected="true"/>    <item android:drawable="@drawable/clr_normal" android:state_selected="false"/></selector>

在values下strings.xml加入颜色代码

  <drawable name="clr_pressed">#ff6501</drawable>   <drawable name="clr_normal">#a44100</drawable>
 

继承ActivityGroup并 加入这行 tabHost.setup(this.getLocalActivityManager());不然会异常,SYActivity为标签页的Activity
MainActivity.java    

<pre name="code" class="java">public class MainActivity extends ActivityGroup  {    String[] title = new String[]{"常用","历史","上次"};      View cyTab,lsTab,scTab;      View[] tabs = new View[]{cyTab,lsTab,scTab};      /** 菜单按钮 */    private Button add_chat;    @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);  TabHost tabHost = (TabHost)findViewById(R.id.tab);          //继承ActivityGroup并写下行代码,不然会异常          tabHost.setup(this.getLocalActivityManager());          for(int i=0;i<tabs.length;i++){              tabs[i] = (View) LayoutInflater.from(this).inflate(R.layout.tabhost, null);              TextView text = (TextView) tabs[i].findViewById(R.id.tab_label);              text.setText(title[i]);                          tabHost.addTab(tabHost.newTabSpec(title[i]).setIndicator(tabs[i]).setContent(new Intent(MainActivity.this, SYActivity.class)));          }  }                   /** 菜单按钮监听 */add_chat = (Button) findViewById(R.id.add_chat);add_chat.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {MainActivity.this.getLocalActivityManager().getCurrentActivity().openOptionsMenu();}});}









0 0
原创粉丝点击