安卓:像那些如何建立标签显示在Android UI页面 [英] Android: How to build tabs like the ones show on Android UI Page

查看:126
本文介绍了安卓:像那些如何建立标签显示在Android UI页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​Android超出其打造为大家使用这个漂亮的UI导轨。但我没有看到任何地方它展示了如何建立这些元素code的例子。

So android goes out of its way to build this nice UI guide for everyone to use. But I don't see anywhere where it shows code examples of how to build these elements.

有关标签的UI指南可以在这里找到。 <一href="http://developer.android.com/design/building-blocks/tabs.html">http://developer.android.com/design/building-blocks/tabs.html.

The UI guidelines for tabs can be found here. http://developer.android.com/design/building-blocks/tabs.html.

有谁知道如何创建标签喜欢这个吗?

Does anyone know how to create tabs likes the this one?

任何帮助将是AP preciated,谢谢。

Any help would be appreciated, thanks.

解决方案张贴
好了,这里是我结束了之后,可能浪费约10小时试图做出一些漂亮的标签做的事情。

SOLUTION POSTED
Ok, so here is what I ended up doing after probably wasting about 10 hours trying to make some good looking tabs.

首先,我报废了使用Android的实现标签的整体思路。由于种种原因选项卡主机部件是假设去pcated的操作栏$ P $,但操作栏只能由机器人3。

First I scrapped the whole idea of using android's implementation of tabs. For one reason the tab host widget is suppose to deprecated for the action bar, but the action bar only works from android 3 on.

我终于想通了,如果使用线性布局,并为背景的线性布局,我把我想用(使用9片图像)的图像。然后,为了把文本超过了的LinearLayout之上创建另一个的LinearLayout和TextView中。然后,让你的线性布局点击。然后,当你得到更高级的,你可以让你的线性布局的背景一个XML选择,你是好去。柜面你没有得到所有的这是我的code。

I finally figured out that if a used a linear layout and as the background for the linear layout i put the image I wanted to use (using a 9 patch image). Then create another linearlayout and textview in order to put text over top of that linearlayout. Then make your linear layout clickable. Then as you get more advanced you can make you linear layout background a xml selector and you are good to go. Incase you didn't get all that here is my code.

的LinearLayout

LinearLayout

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

希望这有助于大家。 Android的标签实在太困难了恼人与它更容易只是为了让我自己从头开始工作。祝你好运!

Hope this helps everyone. Android tabs were just too difficult an annoying to work with that it was easier just to make my own from scratch. Good Luck!

推荐答案

做这样的事情。

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

this is a full working code. enjoy

在某处延长Tabactivity活动的onCreate方法

somewhere in oncreate method of activity extending 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方法为

define setupTab methods as

  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用为

view.xml as

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

和firsttabdrawable.xml在绘制文件夹

and firsttabdrawable.xml in drawable folder as

<?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和thirddrawable.xml

define secondtabdrawable.xml and thirddrawable.xml in the same way

这篇关于安卓:像那些如何建立标签显示在Android UI页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆