Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自定义android选项卡标题中的文本_Java_Android_Android Tabhost - Fatal编程技术网

Java 自定义android选项卡标题中的文本

Java 自定义android选项卡标题中的文本,java,android,android-tabhost,Java,Android,Android Tabhost,我目前的工作是什么:: 我设计了一个标签活动,底部有四个标签 快照 早餐午餐晚餐DividuallistofItems.java public class BreakfastLunchDinnerIndividualListOfItems extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle saved

我目前的工作是什么:

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


快照


早餐午餐晚餐DividuallistofItems.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);


    }

}
<?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>
早餐、午餐、晚餐、个人物品清单。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);


    }

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


问题

  • 请注意,我的第二个选项卡文本是半剪切文本

我正在努力实现的目标


当您在选项卡小部件中定义
布局\u width=“fill\u 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稍作修改。

请注意,不建议在Android上使用底部选项卡。其他平台使用底部选项卡栏在应用程序视图之间切换。根据平台惯例,Android的视图控制选项卡显示在屏幕顶部的操作栏中。此外,Android应用程序可以使用底部栏在拆分动作栏上显示动作。您应该遵循此指导原则,在Android平台上创建与其他应用程序一致的体验,避免在Android上切换操作和视图时出现混淆谢谢您分享此信息!谢谢你。。。。你能给我指一下网上任何给出这个例子的博客或帖子吗?这样我就能看到一个有效的例子。在继续你的设计之前,请在你第一篇帖子的评论中注明@Frank。有关于完全用XML定义选项卡内容的信息,类似于我上面发布的内容。它还有一个包含相应示例java代码的GIT链接。快乐编码!