Android:如何构建Android UI页面上显示的选项卡

Android:如何构建Android UI页面上显示的选项卡,android,android-layout,android-tabhost,android-ui,Android,Android Layout,Android Tabhost,Android Ui,所以android不遗余力地为每个人构建了这个漂亮的用户界面指南。但我看不到任何地方显示了如何构建这些元素的代码示例 选项卡的UI指南可在此处找到 有人知道如何创建像这样的选项卡吗? 任何帮助都将不胜感激,谢谢 已发布解决方案 好的,这就是我在花了大约10个小时制作一些好看的标签后所做的事情。 首先,我放弃了使用android的标签实现的想法。一个原因是,tab主机小部件被认为不推荐用于操作栏,但操作栏仅在android 3上工作 我最终发现,如果a使用了线性布局,并且作为线性布局的背景,我

所以android不遗余力地为每个人构建了这个漂亮的用户界面指南。但我看不到任何地方显示了如何构建这些元素的代码示例

选项卡的UI指南可在此处找到

有人知道如何创建像这样的选项卡吗?

任何帮助都将不胜感激,谢谢

已发布解决方案
好的,这就是我在花了大约10个小时制作一些好看的标签后所做的事情。

首先,我放弃了使用android的标签实现的想法。一个原因是,tab主机小部件被认为不推荐用于操作栏,但操作栏仅在android 3上工作

我最终发现,如果a使用了线性布局,并且作为线性布局的背景,我会放置我想要使用的图像(使用9面片图像)。然后创建另一个linearlayout和textview,以便将文本置于该linearlayout之上。然后使您的线性布局可点击。然后,当您变得更高级时,您可以将线性布局背景设置为xml选择器,这样就可以开始了。万一你没弄明白,这是我的密码

线性布局

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:background="@color/main_screen_bg_color"
    android:orientation="horizontal"
    android:padding="2dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@drawable/selector_not_current"
        android:clickable="true"
        android:onClick="onClickSub"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="Example 1"
                android:textColor="@color/black"
                android:textSize="18sp"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@drawable/selector_current"
        android:clickable="true"
        android:onClick="onClickFoodDetails"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="Example 2"
                android:textColor="@color/black"
                android:textSize="18sp"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

示例选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
      android:drawable="@drawable/selected_pressed_tab" /> <!-- pressed -->
<item android:state_focused="true"
      android:drawable="@drawable/selected_pressed_tab" /> <!-- focused -->
<item android:drawable="@drawable/selected_tab" /> <!-- default -->


希望这对大家都有帮助。安卓标签实在太难了,很难使用,只需从头开始制作自己的标签就更容易了。祝你好运

您需要的选项卡是ActionBar的一部分。具体来说,当ActionBar处于导航选项卡模式时,它们会显示出来

(请参见“添加导航选项卡”下的内容)

您可能想使用www.ActionbarSherlock.com,这是一个库,它将为您提供几乎所有版本的Android上的ActionBar。它的工作原理与官方的相同,并且包括选项卡


不要再使用TabActivity了,它很旧,已被弃用。ActionBar是未来。

像这样做

这是一个完整的工作代码。享受

活动的oncreate方法中的某处扩展Tabactivity

  tabHost = getTabHost();
  Intent intent;
  intent = new Intent().setClass(this, FirstActvity.class);
  setupTab("NearBy", intent, R.drawable.firsttabdrawable);
  intent = new Intent().setClass(this, SecondActivity.class);
  setupTab("History", intent, R.drawable.secondtabdrawable);
  intent = new Intent().setClass(this, ThirdActivity.class);
  setupTab("Setting", intent, R.drawable.thirdtabdrawable);
将setupTab方法定义为

  private void setupTab(String tag, Intent intent, int selectorId) {
  View tabView = LayoutInflater.from(tabHost.getContext()).inflate(R.layout.view, null);
  tabView.setBackgroundResource(selectorId);
  TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabView).setContent(intent);
  tabHost.addTab(setContent);
  }
view.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"
>
</LinearLayout>

以及drawable文件夹中的firsttabdrawable.xml,如下所示

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <!-- When selected, use grey -->
   <item android:drawable="@drawable/selectedfirsttabimage"
      android:state_selected="true" />
   <!-- When not selected, use white-->
   <item android:drawable="@drawable/notselectedfirsttabimage" />
</selector>


以同样的方式定义secondtabdrawable.xml和ThirdRawable.xml,这是使用Holo主题的Android ICS的默认外观。只需让您的设计师为上面的选项卡剪切6幅图像。这似乎很有效,我几乎已经设置好了。当你设计自己的标签图标时,你会使用什么比例?你用什么像素x像素?从屏幕截图中,我得到了120像素x48像素。对吗?