如何为 tabhost 和 tabwidget 创建自定义 api?

发布于 2024-12-09 04:58:18 字数 5742 浏览 0 评论 0原文

我使用tabhost、tabwidget 和horizo​​ntalscrollbar 创建了可滚动选项卡栏,其中layout_gravity 位于底部。现在,我想为此创建自定义 API,这样任何人都可以根据自己的要求使用该 API 更改文本大小、高度、宽度等。

Tabbar.java

package com.tabbar.project;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;

public class Tabbar extends TabActivity {  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);   
        Resources res = getResources();

        // TabHost will have Tabs
        TabHost tabHost = getTabHost();


        /* TabSpec used to create a new tab. 
         * By using TabSpec only we can able to setContent to the tab.
         * By using TabSpec setIndicator() we can set name to tab. 
         * TabSpec setContent() is used to set content for a particular tab.*/

        //adding tabbar items
        TabHost.TabSpec spec;   
        Intent intent;
        intent = new Intent().setClass(this, FirstActivity.class);
        spec = tabHost.newTabSpec("first").setIndicator("Contact", res.getDrawable(R.drawable.contact_img)).setContent(intent);
        tabHost.addTab(spec);



        intent = new Intent().setClass(this, SecondActivity.class);
        spec = tabHost.newTabSpec("second").setIndicator("Dial",  res.getDrawable(R.drawable.dial_img)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, ThirdActivity.class);
        spec = tabHost.newTabSpec("third").setIndicator("Movie", res.getDrawable(R.drawable.movie_img)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, FourthActivity.class);
        spec = tabHost.newTabSpec("fourth").setIndicator("gellary", res.getDrawable(R.drawable.gellary_img)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, FifthActivity.class);
        spec = tabHost.newTabSpec("fifth").setIndicator("Opera", res.getDrawable(R.drawable.opera_img)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, SixthActivity.class);
        spec = tabHost.newTabSpec("sixth").setIndicator("Contacts", res.getDrawable(R.drawable.contact_img)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, SeventhActivity.class);
        spec = tabHost.newTabSpec("seventh").setIndicator("Dial",  res.getDrawable(R.drawable.dial_img)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, EightActivity.class);
        spec = tabHost.newTabSpec("eight").setIndicator("Movie", res.getDrawable(R.drawable.movie_img)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, NinthActivity.class);
        spec = tabHost.newTabSpec("ninth").setIndicator("Gellary", res.getDrawable(R.drawable.gellary_img)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, TenthActivity.class);
        spec = tabHost.newTabSpec("tenth").setIndicator("Opera", res.getDrawable(R.drawable.opera_img)).setContent(intent);
        tabHost.addTab(spec);

        // set the width of tab 
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++){
             tabHost.getTabWidget().getChildAt(i).getLayoutParams().width = 64;
        }



        //provide a method/function for setting height
       // set the Height of tab 
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++){
         tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 60;
       }

        // set the background color of tab (#50000000-transparent,#7392B5) 
         for(int i=0;i<tabHost.getTabWidget().getChildCount();i++){
             tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#50000000")); 
         }

         //tabHost.getTabWidget().getChildAt(0).;
         tabHost.setCurrentTab(0);
    }


}

ma​​in.xml

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp"   
        android:background="@drawable/zero"   
         >
          <TabHost android:id="@android:id/tabhost" xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="fill_parent" 
                 android:layout_height="fill_parent"
                 android:visibility="visible">

            <HorizontalScrollView
            android:layout_width="wrap_content" 
            android:layout_height="fill_parent"
            android:scrollbars="none">   

                 <TabWidget android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:id="@android:id/tabs"
                            android:layout_gravity="bottom"
                            android:tabStripEnabled="false"
                            />


                 </HorizontalScrollView> 

                <FrameLayout android:layout_width="match_parent"
                             android:layout_height="match_parent"
                              android:id="@android:id/tabcontent"/> 

        </TabHost>  


</LinearLayout>

I have created scrollable tab bar, using tabhost, tabwidget, and horizontalscrollbar which is layout_gravity is bottom. Now, i want to create custom API for that, so any one can use the api change the textsize, height, width etc..,according their reqiurment.

Tabbar.java

package com.tabbar.project;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;

public class Tabbar extends TabActivity {  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);   
        Resources res = getResources();

        // TabHost will have Tabs
        TabHost tabHost = getTabHost();


        /* TabSpec used to create a new tab. 
         * By using TabSpec only we can able to setContent to the tab.
         * By using TabSpec setIndicator() we can set name to tab. 
         * TabSpec setContent() is used to set content for a particular tab.*/

        //adding tabbar items
        TabHost.TabSpec spec;   
        Intent intent;
        intent = new Intent().setClass(this, FirstActivity.class);
        spec = tabHost.newTabSpec("first").setIndicator("Contact", res.getDrawable(R.drawable.contact_img)).setContent(intent);
        tabHost.addTab(spec);



        intent = new Intent().setClass(this, SecondActivity.class);
        spec = tabHost.newTabSpec("second").setIndicator("Dial",  res.getDrawable(R.drawable.dial_img)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, ThirdActivity.class);
        spec = tabHost.newTabSpec("third").setIndicator("Movie", res.getDrawable(R.drawable.movie_img)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, FourthActivity.class);
        spec = tabHost.newTabSpec("fourth").setIndicator("gellary", res.getDrawable(R.drawable.gellary_img)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, FifthActivity.class);
        spec = tabHost.newTabSpec("fifth").setIndicator("Opera", res.getDrawable(R.drawable.opera_img)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, SixthActivity.class);
        spec = tabHost.newTabSpec("sixth").setIndicator("Contacts", res.getDrawable(R.drawable.contact_img)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, SeventhActivity.class);
        spec = tabHost.newTabSpec("seventh").setIndicator("Dial",  res.getDrawable(R.drawable.dial_img)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, EightActivity.class);
        spec = tabHost.newTabSpec("eight").setIndicator("Movie", res.getDrawable(R.drawable.movie_img)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, NinthActivity.class);
        spec = tabHost.newTabSpec("ninth").setIndicator("Gellary", res.getDrawable(R.drawable.gellary_img)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, TenthActivity.class);
        spec = tabHost.newTabSpec("tenth").setIndicator("Opera", res.getDrawable(R.drawable.opera_img)).setContent(intent);
        tabHost.addTab(spec);

        // set the width of tab 
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++){
             tabHost.getTabWidget().getChildAt(i).getLayoutParams().width = 64;
        }



        //provide a method/function for setting height
       // set the Height of tab 
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++){
         tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 60;
       }

        // set the background color of tab (#50000000-transparent,#7392B5) 
         for(int i=0;i<tabHost.getTabWidget().getChildCount();i++){
             tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#50000000")); 
         }

         //tabHost.getTabWidget().getChildAt(0).;
         tabHost.setCurrentTab(0);
    }


}

main.xml

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp"   
        android:background="@drawable/zero"   
         >
          <TabHost android:id="@android:id/tabhost" xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="fill_parent" 
                 android:layout_height="fill_parent"
                 android:visibility="visible">

            <HorizontalScrollView
            android:layout_width="wrap_content" 
            android:layout_height="fill_parent"
            android:scrollbars="none">   

                 <TabWidget android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:id="@android:id/tabs"
                            android:layout_gravity="bottom"
                            android:tabStripEnabled="false"
                            />


                 </HorizontalScrollView> 

                <FrameLayout android:layout_width="match_parent"
                             android:layout_height="match_parent"
                              android:id="@android:id/tabcontent"/> 

        </TabHost>  


</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

成熟稳重的好男人 2024-12-16 04:58:18

首先,确定您想要支持的功能列表。看来你已经有了一些想法。然后在您的 Tabbar 类中创建允许人们自定义它的公共方法。

一个想法可能是

public void setTabHeight(int dip) {
    //Code to set tab height goes here
}

这样人们就可以访问您的 Tabber 以及您希望他们拥有的功能。

Tabbar 的功能数量和使用的通用性将取决于您的想象力和设计技巧。

Firstly, decide what list of features you want to support. It seems like you have some ideas already. Then create public methods in your Tabbar class that allow people to customize it.

An idea could be

public void setTabHeight(int dip) {
    //Code to set tab height goes here
}

Then people can have access to your Tabber and the functions you want them to have.

The amount of functionality and the generality of Tabbar's use will be up to your imagination and design skills.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文