2

我需要将标签栏背景设置为白色。此外,我需要将选定的选项卡背景颜色设置为 pink并将其余选项卡(未选择的选项卡)背景颜色设置为blue。我现有的代码如下所示。我需要改变什么?

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:background="#ffffffff"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff" >
    <TabWidget
        android:id="@android:id/tabs"
        android:background="@drawable/tabicon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:background="#ffffffff"
        android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>

tab_indicator.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@drawable/tab_unselected_v4" />
<item android:state_focused="false" android:state_selected="true"
    android:state_pressed="false"
    android:drawable="@drawable/tab_selected_v4" />

<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false"
    android:state_pressed="false"
    android:drawable="@drawable/tab_focus" />
<item android:state_focused="true" android:state_selected="true"
    android:state_pressed="false"
    android:drawable="@drawable/tab_focus" />

<!-- Pressed -->
<item android:state_pressed="true"
    android:drawable="@drawable/tab_press" />
</selector>

tab_unselected_v4.xml:

  <?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
    <shape>
        <gradient android:startColor="#FFFFFF"
            android:endColor="#FFFFFF"
            />

    </shape>
</item>
</selector>

tab_focus.xml:

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- Gradient BgColor for listrow Selected -->
<gradient
  android:startColor="#5e8fb7"
  android:centerColor="#5e8fb7"
  android:endColor="#5e8fb7"
  />
 </shape>

tab_selected.xml:

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--  Gradient Bg for listrow -->
<gradient
  android:startColor="#5e8fb7"
  android:centerColor="#5e8fb7"
  android:endColor="#5e8fb7"
  />
</shape>

tab_press.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--  Gradient Bg for listrow -->
<gradient
  android:startColor="#FFFFFF"
  android:centerColor="#FFFFFF"
  android:endColor="#FFFFFF"
   />
 </shape>

编辑: 我添加了下面的代码。现在我的背景颜色变为白色。但是我选择的项目和未选择的项目颜色没有设置所需的颜色。请帮助我。给我一些好的解决方案。

tabicon.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
    <shape>
        <gradient android:startColor="#FFFFFF"
            android:endColor="#FFFFFF"
            android:angle="90" 
                 />
                 </shape>
                 </item>
         <item android:state_focused="true">
    <shape>
        <gradient android:startColor="#FFFFFF"
            android:endColor="#FFFFFF"
            android:angle="90"  />

    </shape>
</item>
               </selector>

还添加了下面的java代码:

  for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
   {

  tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_indicator);
    }
   tabHost.getTabWidget().setCurrentTab(0);

   tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_indicator);

     }

   public void onTabChanged(String tabId) {

   // TODO Auto-generated method stub
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
       {
   tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_indicator);
    }
  tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab_indicator);
    }
      }

编辑:

在这里我必须设置选定的背景颜色和未选择的背景颜色是成功的。

现在我希望在这里需要一个实现。

一个标签宽度大于第二个和第三个标签宽度。我该怎么做????请帮我。

在这里,仪表板选项卡大于订单和客户选项卡。请帮助我。我该如何设计和开发它。这是我的输出: 电流输出

我希望需要 o/p 如下图所示:

希望需要o/p

编辑:

在这里,我必须在我的style.xml上使用此代码:

  <style name="CustomTheme" parent="@android:style/Theme">
  <item name="android:tabWidgetStyle">@style/CustomTabWidget</item>
  </style>
  <style name="CustomTabWidget" parent="@android:style/Widget.TabWidget">
  <item name="android:textAppearance">@style/CustomTabWidgetText</item>
  </style>
   <style name="CustomTabWidgetText" 
   parent="@android:style/TextAppearance.Widget.TabWidget">
   <item name="android:textSize">20sp</item>
   <item name="android:textStyle">bold</item>
   </style>

之后在 AndroidManifest.xml 文件中添加以下行:

<activity android:name="MyTabActivity" android:theme="@style/CustomTheme">

现在我已经成功地改变了我在 tabhost 上的字体大小、颜色和样式。

4

2 回答 2

0

如果您希望第一个选项卡的宽度大于第二个和第三个选项卡的宽度,您可以为其设置一个单独的 xml 并相应地指定宽度。我说的是这个xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tabsLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_selected_unselected">


</RelativeLayout>

在为标签指示器充气时,您可以使用不同的 xml 为不同的标签指示器充气。

于 2012-10-09T06:50:45.350 回答
0

我认为如果将 TabWidget 放在具有类似于以下白色背景的 LinearLayout 中会更好:

       <LinearLayout
                android:id="@+id/ll_tab_widget"
                android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                android:background="@android:color/white" >

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

        </LinearLayout>

此外,为了设置选中和未选中的颜色,您可以创建一个带有 RelativeLayout 的 xml,其背景是选择器(选中和未选中状态具有不同的图像或颜色),如下所示:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tabsLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_selected_unselected">


</RelativeLayout>

然后,您可以通过上述 xml 扩展 TabHost.TabSpec 指标。

如果您发现任何困难,您可以发布相同的内容。一切顺利!

于 2012-10-08T11:39:42.237 回答