繁体   English   中英

在Android的标签标题中自定义文本

[英]Customizing Text in tab title for android

我目前的工作是什么……

我设计了一个标签活动,底部有四个标签


快照

在此处输入图片说明


BreakfastLunchDinnerIndividualListOfItems.java

public class BreakfastLunchDinnerIndividualListOfItems extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.breakfast_lunch_dinner_individual_list_of_items);

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

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, BLD_IndividualListOfItems_Starters.class);
        spec = tabHost.newTabSpec("STARTERS").setIndicator("Starters").setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs

        intent = new Intent().setClass(this, BLD_IndividualListOfItems_MainCourse.class);
        spec = tabHost.newTabSpec("MAIN_COURSE").setIndicator("Main Course").setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, BLD_IndividualListOfItems_SideCourse.class);
        spec = tabHost.newTabSpec("SIDE_COURSE").setIndicator("Side course").setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, BLD_IndividualListOfItems_Others.class);
        spec = tabHost.newTabSpec("OTHERS").setIndicator("Others").setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, BLD_IndividualListOfItems_Desert.class);
        spec = tabHost.newTabSpec("DESERT").setIndicator("Deserts").setContent(intent);
        tabHost.addTab(spec);

        //set tab which one you want open first time 0 or 1 or 2
        tabHost.setCurrentTab(0);


    }

}

breakfast_lunch_dinner_individual_list_of_items.java

<?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"
            android:layout_marginBottom="-4dp"/>

    </LinearLayout>

</TabHost>

问题 ::

  • 请注意,我的第二个制表符文本为半切口

我正在努力实现的目标 ::

在此处输入图片说明

当您在标签小部件中定义layout_width="fill_parent"时,就是说所有标签的尺寸均等,并填充其各自的父标签。 但是,对于您而言,这些标签内的内容不能保证是均匀的。 因此,您需要在每个标签内创建一个新的TextView来容纳文本。 这将允许特定于每个选项卡内容的内容包装。

我提供了一个示例XML进行说明:

<?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"
        android:layout_marginBottom="-4dp">

            <TextView
                android:tag="tab0"
                android:text="Starters"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                />
            <TextView
                android:tag="tab1"
                android:text="Main Course"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                />
            <TextView
                android:tag="tab2"
                android:text="Side Course"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                />
            <TextView
                android:tag="tab2"
                android:text="Others"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                />
            <TextView
                android:tag="tab2"
                android:text="Desert"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                />
    </TabWidget>
</LinearLayout>

我相信这也将需要对Java稍作修改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM