安卓:自定义标签的使用TabHost和放大器的样子; TabWidget自定义、放大器、样子、标签

2023-09-04 11:53:40 作者:抢钱抢粮抢娘们

我以前运行结束关于这个职位,但我觉得我现在可以(阅读一些其他职位后)更好地解释我想要什么,并改写它,所以它会更好地理解。

I opend a post about this before but I feel that I can now (after reading some other posts) better explain what I want and rephrase it so it will be better understand.

我跟有关标签布局的开发指导教程,我设法与它创建的标签,但我希望做一些定制它(我没有看其他职位,但无论是code有许多失误给它或它没有回答我要找的)。

I followed the tutorial about Tab Layout on the dev guide and I managed to create tabs with it, but I want to do some customization to it (and I did look on other posts, but either the code had many mistakes to it or it didn't answer what I'm looking for).

第一个问题我已经是该测试是在大多数情况下移到图标上,而不是在它下面(我用的尺寸为48×48的图标,建议在开发指南)。我想要使​​用像WRAP_CONTENT做的标签。 我也想改变文字大小(我认为这就是所谓的标签)。

The first problem I have is that the test is in most part over the icon instead of below it (I used an icon with dimensions 48x48 as recommended on the dev guide). I want the tab with to act like wrap_content does. I also want to change the text size (I think it's called the label).

我想用十六进制三胞胎更改标签的背景色,以之间的情况下进行更改:在该选项卡中选择一个,当它不是。

I want to use hex triplets to change the background color of the tabs, to change it between to situations : when this tab is the one selected and when it's not.

我希望能够改变这种状况是标签下方的线的颜色,我无法找到如何做到这一点的任何信息。

I want to be able to change the color of the line that is below the tabs, I could not find any information on how to do this.

我目前使用来创建一个新的选项卡中的code是(从开发指南):

The code I'm currently using to create a new tab is (from the dev guide):

    intent = new Intent().setClass(this, GroupsActivity.class);
    spec = tabHost.newTabSpec("groups").setIndicator("groups",
                      res.getDrawable(R.drawable.ic_tab_groups))
                  .setContent(intent);
    tabHost.addTab(spec);

(组是标签名称)。

(groups is the tab name).

帮助是非常AP preciated!

Help is very much appreciated!

推荐答案

而不是试图以自定义窗口小部件标签本身,这里要说的是我已经成功地用于一个项目,可以节省你有些头疼了另一种方式:

Rather than trying to customize the widget tabs themselves, here is an alternate approach that I've used successfully on a project that may save you some headaches:

我们的想法是使用隐藏TabWidget在布局中并用含有按钮定制的LinearLayout控制它。通过这种方式,可以更轻松地自定义的按钮来看看,但是你会喜欢。你会控制每个按钮的OnClick内的活动的实际TabWidget。

The idea is to use a hidden TabWidget in your layout and control it with a customized LinearLayout containing Buttons. This way, you can more easily customize the buttons to look however you'd like. You'll control the actual TabWidget in your Activity within each button's OnClick.

创建具有两个TabWidget和按钮布局:

Create your layout with both the TabWidget and the Buttons:

    <?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">

    <RelativeLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:gravity="bottom">
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:visibility="gone" />

        <LinearLayout android:id="@+id/tabbar"
            android:orientation="horizontal" android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button android:id="@+id/firstButton"
                android:layout_alignParentTop="true" android:background="@drawable/btn_first_on"
                android:layout_width="100dp" android:layout_height="43dp"
                android:clickable="true"></Button>
            <Button android:id="@+id/secondButton"
                android:layout_alignParentTop="true" android:background="@drawable/btn_second_off"
                android:layout_height="43dp" android:layout_width="100dp"
                android:clickable="true"></Button>
            <Button android:id="@+id/thirdButton"
                android:layout_alignParentTop="true" android:background="@drawable/btn_third_off"
                android:layout_height="43dp" android:layout_width="100dp"
                android:clickable="true"></Button>
            <Button android:id="@+id/forthButton"
                android:layout_alignParentTop="true" android:background="@drawable/btn_forth_off"
                android:layout_height="43dp" android:layout_width="100dp"
                android:clickable="true"></Button>
        </LinearLayout>

        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:layout_below="@+id/tabbar" />

    </RelativeLayout>
</TabHost>

设置你的活动来处理使用按钮调节选项卡享有起来的onCreate:

Android中的TabHost应用时无法改变tab上的字体颜色是咋回事 字体大小可以调整,颜色只变黑,代码如下

Set up the onCreate of your activity to handle using the buttons for adjusting the tab views:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // tabs        
        firstButton = (Button) findViewById(R.id.firstButton);
        secondButton = (Button) findViewById(R.id.secondButton);        
        thirdButton = (Button) findViewById(R.id.thirdButton);
        forthButton = (Button) findViewById(R.id.forthButton);

        Resources res = getResources(); // Resource object to get Drawables
        final TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        intent = new Intent().setClass(this, FirstGroupActivity.class);
        spec = tabHost.newTabSpec("first").setIndicator("First").setContent(intent);
        tabHost.addTab(spec);
        intent = new Intent().setClass(this, SecondGroupActivity.class);
        spec = tabHost.newTabSpec("second").setIndicator("Second").setContent(intent);
        tabHost.addTab(spec);   

        intent = new Intent().setClass(this, ThirdGroupActivity.class);
        spec = tabHost.newTabSpec("third").setIndicator("Third").setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, ForthActivity.class);
        spec = tabHost.newTabSpec("forth").setIndicator("Forth").setContent(intent);
        tabHost.addTab(spec);


        tabHost.setCurrentTab(0);

        firstButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v)
            {
                tabHost.setCurrentTab(0);
                firstButton.setBackgroundResource(R.drawable.btn_first_on);
                secondButton.setBackgroundResource(R.drawable.btn_second_off);              
                thirdButton.setBackgroundResource(R.drawable.btn_third_off);
                forthButton.setBackgroundResource(R.drawable.btn_forth_off);            
            }

        });


        secondButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v)
            {
                tabHost.setCurrentTab(1);
                firstButton.setBackgroundResource(R.drawable.btn_first_off);
                secondButton.setBackgroundResource(R.drawable.btn_second_on);                       
                thirdButton.setBackgroundResource(R.drawable.btn_third_off);                        
                forthButton.setBackgroundResource(R.drawable.btn_forth_off);

            }

        });


        thirdButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v)
            {
                tabHost.setCurrentTab(3);
                firstButton.setBackgroundResource(R.drawable.btn_first_off);
                secondButton.setBackgroundResource(R.drawable.btn_second_off);              
                thirdButton.setBackgroundResource(R.drawable.btn_third_on);
                forthButton.setBackgroundResource(R.drawable.btn_forth_off);

            }

        });


        forthButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v)
            {
                tabHost.setCurrentTab(4);
                firstButton.setBackgroundResource(R.drawable.btn_first_off);
                secondButton.setBackgroundResource(R.drawable.btn_second_off);              
                thirdButton.setBackgroundResource(R.drawable.btn_third_off);
                forthButton.setBackgroundResource(R.drawable.btn_forth_on);

            }

        });
    }

正如你所看到的,我使用的是可绘制的按钮和关闭图像。使用这种技术,你不局限于单纯的时候只是想自定义TabWidget的选项卡的外观,你可以创建一个完全自定义的期待您的卡中的可用选项。

As you can see, I'm using drawables for the images of the buttons on and off. Using this technique, you're not limited to the options available when simply just trying to customize the look of the TabWidget's tabs and you can create a completely custom look to your tabs.