gpt4 book ai didi

android - 在 Android 中创建自定义选项卡

转载 作者:行者123 更新时间:2023-11-29 01:48:44 25 4
gpt4 key购买 nike

我想在 Andorid 中创建自定义标签。我搜索了很多但没有找到任何相关的东西。我想创建这样的标签 enter image description here

我的代码生成这个

enter image description here

我的代码是这样的

CustomTabActivity.java

public class CustomTabActivity extends TabActivity {

private TabHost mTabHost;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = getTabHost();
mTabHost.setup();
Intent intentAndroid1 = new Intent().setClass(this,Tab1.class);
TabSpec tab1 = mTabHost
.newTabSpec("Android")
.setIndicator("",getResources().getDrawable(R.drawable.icon))
.setContent(intentAndroid1);


Intent intentAndroid2 = new Intent().setClass(this,Tab2.class);
TabSpec tab2 = mTabHost
.newTabSpec("Android")
.setIndicator("",getResources().getDrawable(R.drawable.icon))
.setContent(intentAndroid2);
mTabHost.addTab(tab1);
mTabHost.addTab(tab2);
}
}

主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>

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

</LinearLayout>

最佳答案

如果您只想使用选项卡名称而不是选项卡的图标/图像,只需更改 TabSpec.setIndicator() 方法即可。

像这样:

Intent intentAndroid1 = new Intent().setClass(this, Tab1.class);
TabHost.TabSpec tab1 = mTabHost
.newTabSpec("Android")
.setIndicator("Rankings")
.setContent(intentAndroid1);


Intent intentAndroid2 = new Intent().setClass(this, Tab2.class);
TabHost.TabSpec tab2 = mTabHost
.newTabSpec("Android")
.setIndicator("My Team")
.setContent(intentAndroid2);

产生这样的东西(Android 4.3):

enter image description here

有关 TabHost.TabSpec 的更多信息,请查看 Android 开发人员中心文档:http://developer.android.com/reference/android/widget/TabHost.TabSpec.html

关于android - 在 Android 中创建自定义选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19729554/

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