An example method for Android custom Tabactivity _android

Source: Internet
Author: User


First, change the tab bar position.
Java code. Add in Tabactivity's OnCreate method
Setcontentview (R.layout.tab_host);

The Layout tab_host.xml is extracted from the system resource file and slightly modified.
The original Tab_host.xml contents of the system are as follows

Copy Code code 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");
* * may not use this file except in compliance with the License.
* * Obtain a copy of the License at
**
* * http://www.apache.org/licenses/LICENSE-2.0
**
* * unless required by applicable and agreed to in writing, software
* * Distributed under the License is distributed on ' as ' basis,
* * without warranties or CONDITIONS of any KIND, either express or implied.
* * The License for the specific language governing permissions and
* * Limitations under the License.
*/
-->

<tabhost xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@android: Id/tabhost"
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:id= "@android: Id/tabs" android:layout_width= "Match_parent"
android:layout_height= "wrap_content" android:layout_weight= "0"/>
<framelayout android:id= "@android: Id/tabcontent"
Android:layout_width= "Match_parent" android:layout_height= "0dip"
android:layout_weight= "1"/>
</LinearLayout>
</TabHost>

To implement the tab bar below the page, simply modify it.

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<tabhost xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@android: Id/tabhost"
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:id= "@android: Id/tabcontent"
Android:layout_width= "Fill_parent" android:layout_height= "0dip"
android:layout_weight= "1"/>
<tabwidget android:id= "@android: Id/tabs" android:layout_width= "Fill_parent"
android:layout_height= "wrap_content" android:layout_weight= "0"/>
</LinearLayout>
</TabHost>

In this way, the tab bar is implemented on the page under the book. It should be noted that the ID of the view is not modified.

Second, the Custom tab of the picture. The Tab_indicator.xml content of the system is as follows

Copy Code code 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");
     may not use this file except in compliance with the License.
     may obtain a copy of the License at

       & nbsp;  http://www.apache.org/licenses/LICENSE-2.0

     unless required by Applicable or agreed to into writing, software
     distributed under the License is distributed On ' as is ' basis,
     without warranties or CONDITIONS to any KIND, either express OR implied.< br>     the License for the specific language governing permissions and
   & nbsp Limitations under the License.
;

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
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:d rawable/tab_indicator" >

<imageview android:id= "@+id/icon"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_centerhorizontal= "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>

As you can see, by default, icons are above the text and cannot occupy the entire grid, and cannot meet the design needs. You can therefore override the layout.
Write Tab_in.xml

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Wrap_content"
android:layout_height= "64dip"
android:orientation= "Vertical"
>
</RelativeLayout>

View view1 = inflater.inflate (r.layout.tab_in, NULL);
View view2 = inflater.inflate (r.layout.tab_in, NULL);
View view3 = inflater.inflate (r.layout.tab_in, NULL);



View1. Setbackgroundresource (r.drawable.record_upload_button_stateful);
View2. Setbackgroundresource (r.drawable.record_download_button_stateful);
View3. Setbackgroundresource (r.drawable.record_receive_button_stateful);
Tabhost.addtab (tabhost
. Newtabspec ("View1")
. Setindicator (View1)
);

Tabhost.addtab (tabhost
. Newtabspec ("View2")
. Setindicator (VIEW2)
);


Tabhost.addtab (tabhost
. Newtabspec ("VIEW3")
. Setindicator (VIEW3)
);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.