SoFunction
Updated on 2025-04-09

Android Custom TabActivity Instance Method


1. Change the position of the Tab column.
java code. Add in the oncreate method of TabActivity
setContentView(.tab_host);

Among them, Layout tab_host.xml is cut out from the system resource file and made slightly modified.
The original tab_host.xml content of the system is as follows

Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/layout/tab_content.xml
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     /licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->

<TabHost xmlns:andro android:
    android:layout_width="match_parent" android:layout_height="match_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_width="match_parent" android:layout_height="match_parent">
        <TabWidget android: android:layout_width="match_parent"
            android:layout_height="wrap_content" android:layout_weight="0" />
        <FrameLayout android:
            android:layout_width="match_parent" android:layout_height="0dip"
            android:layout_weight="1"/>
    </LinearLayout>
</TabHost>

To implement the TAB column at the bottom of the page, just make a simple modification.

Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:andro android:
         android:layout_width="fill_parent" android:layout_height="fill_parent">
        <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
                 <FrameLayout android:
                 android:layout_width="fill_parent" android:layout_height="0dip"
            android:layout_weight="1"/>
       <TabWidget android: android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="0" />
         </LinearLayout>
 </TabHost>

In this way, the TAB column is realized in the lower volume of the page. It should be noted that the view id should not be modified.

2. Customize the pictures of TAB. The contents of the tab_indicator.xml that comes with the system are as follows

Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          /licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<RelativeLayout xmlns:andro
    android:layout_width="0dip"
    android:layout_height="64dip"
    android:layout_weight="1"
    android:layout_marginLeft="-3dip"
    android:layout_marginRight="-3dip"
    android:orientation="vertical"
    android:background="@android:drawable/tab_indicator">

    <ImageView android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
    />

    <TextView android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        style="?android:attr/tabWidgetStyle"
    />

</RelativeLayout>

It can be seen that by default, the icon is above the text and cannot occupy the entire grid, which cannot meet the design needs. Therefore, the Layout can be rewritten.
Write tab_in.xml

Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:andro
     android:layout_width="wrap_content"
     android:layout_height="64dip"
     android:orientation="vertical"
     >
</RelativeLayout>

View view1 = (.tab_in, null);;
        View view2 = (.tab_in, null);;
        View view3 = (.tab_in, null);;

       

        view1 .setBackgroundResource(.record_upload_button_stateful);
        view2 .setBackgroundResource(.record_download_button_stateful);
        view3 .setBackgroundResource(.record_receive_button_stateful);
(tabHost
                .newTabSpec("view1")
                .setIndicator(view1)             
          );

        (tabHost
                .newTabSpec("view2")
                .setIndicator(view2)
        );

      
        (tabHost
                .newTabSpec("view3")
                .setIndicator(view3)
             );