2

您好,我有一个 tabhost 和一个全息主题,我试图更改您单击的选项卡下的蓝色,但没有任何成功。这是 main.xml

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

    <LinearLayout
                android:id="@+id/tabhost_parent"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

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

            <LinearLayout
                android:id="@+id/tabhost_lierlayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/regi_input02_tap"
                    android:showDividers="middle">

                </TabWidget>

                 <LinearLayout 
                    android:id="@+id/header_layout"
                    android:layout_width="match_parent"
                    android:layout_height="32dp"
                    android:orientation="horizontal" 
                    android:background="@android:color/background_dark">

                     <TextView
                        android:layout_width="wrap_content" 
                        android:layout_height="wrap_content"
                        android:text="Test Text"  
                        android:textSize="23dp" 
                        android:textColor="@android:color/background_light"
                     />
                </LinearLayout>


                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" 
                    android:padding="5dp">


                    <LinearLayout
                        android:id="@+id/sports_tab"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >

                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/busines_tab"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
                        <TextView 
                             android:layout_width="fill_parent" 
                             android:layout_height="wrap_content"
                             android:text="busines"   
                        />

                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/news_tab"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
                        <TextView 
                             android:layout_width="fill_parent" 
                             android:layout_height="wrap_content"
                             android:text="news"   
                        />
                    </LinearLayout>
                </FrameLayout>
            </LinearLayout>
         </TabHost>
    </LinearLayout>
</LinearLayout>

图片链接

它看起来类似于上面链接中的图片,我要做的就是更改 tab1 下的蓝色条纹。

我被搜索了大约 2 天,但没有找到任何关于如何执行此操作的好的解释。

我试图玩
这样的资源:

这是 manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.brodgate.mivzaki"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="14" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CustomTheme" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

这是我试图为蓝线改变的风格,但没有成功

样式.xml

<style name="CustomTheme" parent="android:Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/ActionBar</item>
</style>

<style name="ActionBar" parent="android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@color/action_bar_background</item>
    <item name="android:showDividers">beginning</item>
    <item name="android:divider">@color/action_bar_divider</item>
</style>

我想要的只是改变线条颜色,我们将不胜感激。

4

1 回答 1

1

因为你有一个 TabHost,所以它比使用 ActionBar 的选项卡要复杂得多。我们为我们的应用解决的解决方案是为标签指示器自定义框架 9 补丁图像。由于似乎没有办法引用自定义选项卡指示器,因此我们也必须复制选项卡视图并在为 TabHost 创建选项卡时使用它。您必须下载完整的框架源代码才能获取其中一些文件。这是您需要做的:

1) 从框架源中复制文件 /data/res/drawable/tab_indicator_holo.xml。

2) 复制 tab_indicator_holo.xml 中引用的 9-patches 并修改它们以适合您的应用程序。对于要包含的每个密度,您需要以下文件:

  • tab_selected_pressed_holo.9.png
  • tab_unselected_pressed_holo.9.png
  • tab_selected_focused_holo.9.png
  • tab_unselected_focused_holo.9.png
  • tab_selected_holo.9.png
  • tab_unselected_holo.9.png

3)创建自定义选项卡样式:

<style name="MyCustomTabView" parent="@android:style/Widget.Holo.Tab">
    <item name="android:background">@drawable/tab_indicator_holo</item>
</style>

4)从框架创建标签视图布局的副本:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="?android:attr/actionBarSize"
    android:orientation="horizontal"
    style="@style/MyCustomTabView">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:paddingTop="12dip"
        android:paddingBottom="12dip"
        android:paddingLeft="12dip"
        android:paddingRight="12dip"
        android:visibility="gone"/>

    <TextView
        style="@android:style/Widget.Holo.ActionBar.TabText"
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:visibility="gone"/>

</LinearLayout>

5) 最后一步是在您的活动代码中,当您创建选项卡时,您将指示器设置为您从上述布局创建的视图。这就是它的样子(这是 ApiDemos 示例应用程序中的 Tabs1.java,经过修改以创建自定义指标视图):

/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.view;

import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.view.LayoutInflater;

import com.example.android.apis.R;

/**
 * An example of tabs that uses labels ({@link TabSpec#setIndicator(CharSequence)})
 * for its indicators and views by id from a layout file ({@link TabSpec#setContent(int)}).
 */
public class Tabs1 extends TabActivity {

    private View createTabIndicator(Drawable icon, String title) {
        if (mInflater == null) {
            mInflater = getLayoutInflater();
        }

        View view = mInflater.inflate(R.layout.tab_view, null);

        if (icon != null) {
            ImageView imageView = (ImageView) view.findViewById(R.id.icon);
            imageView.setImageDrawable(icon);
            imageView.setVisibility(View.VISIBLE);
        }

        if (title != null) {
            TextView textView = (TextView) view.findViewById(R.id.title);
            textView.setText(title);
            textView.setVisibility(View.VISIBLE);
        }

        return view;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TabHost tabHost = getTabHost();

        LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);

        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator(createTabIndicator(null, "tab1"))
                .setContent(R.id.view1));
        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator(createTabIndicator(null, "tab2"))
                .setContent(R.id.view2));
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator(createTabIndicator(null, "tab3"))
                .setContent(R.id.view3));
    }
}

如果您不需要确切的 Holo 外观,则可以稍微简化一下。希望这可以帮助!

于 2012-12-23T21:22:56.477 回答