0

我已经使用 Objective-C 在 iOS 上构建了一个标签栏,并试图使用 Titanium 让 Android 上的标签栏看起来尽可能接近。我在想:

  1. 你如何给标签栏本身一个背景图片?我唯一能做的就是给它一个背景颜色。该backgroundImage属性没有任何影响。

  2. 如何更改选定选项卡的背景颜色?我试过改变activeTabBackgroundSelectedColor没有运气。

  3. 你如何修改活动标签的图标?我希望图标在未选中时为灰色,但在选中时为白色。我尝试创建一个可绘制对象,但我无法让 Android 应用程序识别它,Titanium.App.Android.R.drawable.ic_tab_coupon 感谢您的帮助!

-

PS 对于它的价值,这是我正在使用的代码片段。请注意,我还有其他以相同方式实例化/添加的选项卡,为了简洁起见,我没有显示它们。

var self = Ti.UI.createTabGroup({
    backgroundColor: 'red',
    activeTabBackgroundColor: 'blue',
});

var win1 = new Window(L('SpintoWin'));

var tab1 = Ti.UI.createTab({
    backgroundColor: 'none',
    title: L('SpintoWin'),
    icon: '/images/tab-icon-gift.png',
    window: win1
});

win1.containingTab = tab1;

self.addTab(tab1);
4

2 回答 2

0

1.你需要定义TabWidget'sandroid:background属性。例如:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@android:id/tabs" />

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"

        android:background="@drawable/tab_backgrnd" />

</RelativeLayout>

哪里tab_backgrnd可能有任何drawable

2 & 3.首先,您像 Android 本身一样定义选项卡的布局。以下是 Android 的选项卡布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dip"
    android:layout_height="64dip"
    android:layout_weight="1"
    android:layout_marginLeft="-3dip"
    android:layout_marginRight="-3dip"
    android:orientation="vertical"
    android:background="@drawable/tab_indicator">

    <ImageView android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        style="?android:attr/tabWidgetStyle" />

</RelativeLayout>

您可以在此布局中放入您想要的任何东西,而不仅仅是 oneImageView和 one TextView。要使您的选项卡在按下/选择时发生变化,请将 设置StateListDrawable为根布局的背景和选项卡包含的其他元素的背景(ImageViewTextView上面的示例中)。

然后你膨胀这个布局并将其设置为选项卡指示器:

    TabHost tabHost = getTabHost();
    TabHost.TabSpec tabSpec;  // Reusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab
    View tabView;
    LayoutInflater inflater = getLayoutInflater();

    intent = new Intent().setClass(this, YourActivity1.class);
    tabView = inflater.inflate(R.layout.tab_layout, tabHost.getTabWidget(), false);
    tabSpec = tabHost.newTabSpec(TAG_TAB_1).setIndicator(tabView).setContent(intent);
    tabHost.addTab(tabSpec);

    // Do the same for the other tabs
    ...

您可以通过编程方式设置每个选项卡的图标和文本(就像 Android 使用android:id="@+id/icon"android:id="@+id/title"ids 一样),或者您可以为每个选项卡定义单独的布局。

于 2012-07-05T18:03:40.067 回答
0

这是一个较老的问题,但我想我会提供一些有关此问题的其他详细信息,因为该问题在搜索引擎结果中的条款相当高。

http://developer.appcelerator.com/question/53851/android-tab-icon-states

在 Titanium 1.7 中,选项卡被修改为支持可绘制资源以允许这样做(见下文,包括示例代码)

https://jira.appcelerator.org/browse/TIMOB-3179

现在,在那之后的某处修复了将可绘制资源指定为选项卡图标的能力被删除。

下一个合乎逻辑的步骤是尝试在.icon选项卡获得或失去焦点时修改选项卡的属性,如以下问题所述:

http://developer.appcelerator.com/question/135063/how-to-change-active-tab-icon

由于原生 Android 代码采用可绘制资源而不是字符串值,因此选项卡上的图标在绘制后无法更新:

https://jira.appcelerator.org/browse/TIMOB-9962

所以,这个问题的答案是:

  • 您不能将图标设置为可绘制资源
  • 绘制选项卡后,您无法更改选项卡图标

在使用 Ti.UI.TabGroup / Ti.UI.Tab 时,在上述任一选项可用之前,您将无法更改选项卡的图标。

于 2013-02-15T21:20:04.870 回答