gpt4 book ai didi

java - 为 android 自定义选项卡标题中的文本

转载 作者:行者123 更新时间:2023-11-29 21:20:12 25 4
gpt4 key购买 nike

我目前的工作::

我设计了一个底部有四个标签的标签 Activity


快照

enter image description here


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>

问题::

  • 请注意我的第二个标签文本被截掉了一半

我想要实现的目标::

enter image description here

最佳答案

当您在选项卡小部件中定义 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 进行轻微修改。

关于java - 为 android 自定义选项卡标题中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20822710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com