FragmentTabHost无法更改所选背景

时间:2014-08-03 19:56:22

标签: android xml android-fragments tabs android-tabhost

我一直在尝试编辑FragmentedTabHost的背景颜色,以便稍微弄乱它,我完全不知道为什么我可以改变"未选择的"状态但不是"选择"状态。

所以我所拥有的是一个名为"标签"在里面我引用了另外两个" tab_selected"和" tab_unselected"。我几乎在所有可以找到的地方应用了这个背景,看看它是否有效,但我认为最好的位置是在TabWidget标签中。

选定状态保持正常选项卡的浅灰色,但另一个实际更改。

我不知道我是否应该在其他地方更改代码,因为我已经在代码中更改了代码:

mTabHost.setBackgroundResource(R.drawable.tabs);

仍然没有进展......

以下是代码:

tabs.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
    android:drawable="@drawable/tab_selected" />
<item android:state_selected="false"
    android:drawable="@drawable/tab_unselected" />
</selector>

tab_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="#7fff00" />
    </shape>
</item>
<item android:top="20dp">
    <shape android:shape="rectangle">
        <solid android:color="#7fff00" />
    </shape>
</item>
<item android:bottom="2dp">
    <shape  android:shape="rectangle">
        <gradient android:startColor="#7fff00"
            android:endColor="#7fff00"
            android:angle="270" />
        <corners android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
    </shape>
</item>
</layer-list>

tab_unselected.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="#86B4CF" />
    </shape>
</item>
<item android:top="20dp">
    <shape android:shape="rectangle">
        <solid android:color="#86B4CF" />
    </shape>
</item>
<item android:bottom="2dp">
    <shape  android:shape="rectangle">
        <gradient android:startColor="#86B4CF"
            android:endColor="#6792AB"
            android:angle="270" />
        <corners android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
    </shape>
</item>
</layer-list>

最后是FragmentedHost:

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".Home"
android:orientation="vertical"
android:background="@drawable/global_background">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_date"
        style="@style/header"/>

</LinearLayout>

<android.support.v4.app.FragmentTabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:background="@drawable/tabs"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

有一种名为setOnTabChangedListener的方法。我希望这会对你有所帮助

mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                setTabColor(mTabHost);
            }
        });

setTabColor(mTabHost) 功能

    public static void setTabColor(TabHost tabhost) {
    for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
        tabhost.getTabWidget().getChildAt(i)
                .setBackgroundColor(Color.parseColor("#434a54")); // unselected
    }
    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab())
            .setBackgroundColor(Color.parseColor("#22262c")); // selected
}

答案 1 :(得分:0)

虽然我决定采取另一种方式,但感谢你们的回答。

由于不是使用FragmentedTabHost,而是决定使用ActionBar Tab,因为它是最推荐的。

为此,我创建了这个类:

public class ActionBarTabListener implements ActionBar.TabListener {
public Fragment fragment;

public ActionBarTabListener(Fragment fragment) {
    this.fragment = fragment;
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    //do what you want when tab is reselected, I do nothing
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    ft.replace(R.id.fragment_placeholder, fragment);
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    ft.remove(fragment);
}
}

更改了我的主类(包含所有标签的主类):

public class EntryDetails extends ActionBarActivity {

private Entry entry;

//private FragmentTabHost mTabHost;
private TextView tv_date;
private ActionBar ab;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_entry_details);

    ab = getSupportActionBar();

    ab.setTitle("Ecz :: Entry Details");
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    entry = getIntent().getExtras().getParcelable("e");

    /* TAB ITCHES */

    Bundle i = new Bundle();
    i.putParcelableArrayList("i", ArrayListHelper.OrderItchesByTime(entry.getItches()));

    Fragment f = new TabItches();
    f.setArguments(i);

    ActionBar.Tab tab = ab.newTab().setText("Itches").setTabListener(new ActionBarTabListener(f)).setTag(i);
    ab.addTab(tab);

    /* TAB TREATMENTS */

    i = new Bundle();
    i.putParcelableArrayList("t", ArrayListHelper.OrderTreatmentsByTime(entry.getTreatments()));

    f = new TabTreatments();
    f.setArguments(i);

    tab = ab.newTab().setText("Treatments").setTabListener(new ActionBarTabListener(f)).setTag(i);
    ab.addTab(tab);

    tv_date = (TextView) findViewById(R.id.tv_date);

    tv_date.setText(entry.getDate());
}

}

然后我在EntryDetails.xml中更改了我的XML

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".Home"
android:orientation="vertical"
android:background="@drawable/global_background">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_date"
        style="@style/header"/>

</LinearLayout>

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

</LinearLayout>

所以现在我只有linearlayout而且没有tabhost。

为了让您更深入地了解我将代码放在我的TabItches下面:

public class TabItches extends ListFragment{

private ArrayList<Itch> itches;
public ListAdapter itchesAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.tab_layout_itches, container, false);

    this.setRetainInstance(true);

    itches = this.getArguments().getParcelableArrayList("i");

    itchesAdapter = new ItchAdapter(getActivity(), itches);

    setListAdapter(itchesAdapter);

    return view;
}
}

要编辑我的标签的外观,我现在必须做更多的事情! 此代码属于&#34; @ style&#34;

<style name="Appy" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/AppyActionBar</item>
    <item name="actionBarStyle">@style/AppyActionBar</item>

    <item name="android:actionBarTabStyle">@style/AppyActionBarTab</item>
    <item name="actionBarTabStyle">@style/AppyActionBarTab</item>
</style>

<!-- ActionBar styles -->
<style name="AppyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/AppyBlue</item>
    <!-- Support library compatibility -->
    <item name="background">@color/AppyBlue</item>
</style>

<style name="AppyActionBarTab"    parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@drawable/actionbar_tab_background</item>
    <!-- Support library compatibility -->
    <item name="background">@drawable/actionbar_tab_background</item>

    <item name="android:gravity">center</item>
</style>

只考虑&#34; AppyActionBarTab&#34;代码位请!

actionbar_tab_background.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@color/AppyDeeperBlue"/>
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/actionbar_tab_background_selected"/>
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/actionbar_tab_background_selected_pressed"/>
</selector>

actionbar_tab_selected.xml

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-5dp" android:left="-5dp" android:right="-5dp">
    <shape android:shape="rectangle">
        <stroke android:color="@color/AppyBlue" android:width="5dp"/>
        <solid android:color="@color/AppyDeeperBlue"/>
    </shape>
</item>
</layer-list>

actionbar_tab_selected_pressed.xml

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-5dp" android:left="-5dp" android:right="-5dp">
    <shape android:shape="rectangle">
        <stroke android:color="#B8D0DE" android:width="5dp" />
        <solid android:color="@color/AppyDeeperBlue"/>
    </shape>
</item>
</layer-list>

如果我设法再次找到它们,我会在StackOverFlow的所有帖子中自我创建我会引用它们。

现在我正在努力改变API 10中的分频器颜色,我似乎无法绕过它! :d

我希望答案足够完整,如果有任何问题只是写,我会尽力回答!!

修改的 用分隔符的颜色修复了我的问题。你需要做的是改变分隔线的可绘制性。

action_tab_divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/AppyBlue" />
<stroke android:width="5dp"
    android:color="@color/AppyBlue"/>
</shape>

现在您需要将其应用于样式

<style name="Appy" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/AppyActionBar</item>
    <item name="actionBarStyle">@style/AppyActionBar</item>

    <item name="android:actionBarTabStyle">@style/AppyActionBarTab</item>
    <item name="actionBarTabStyle">@style/AppyActionBarTab</item>

    <item name="android:actionBarDivider">@drawable/actionbar_tab_divider</item>
    <item name="actionBarDivider">@drawable/actionbar_tab_divider</item>
</style>

请注意,我们正在覆盖父母&#34;风格,而不是上面两个中的任何一个!

感谢您阅读:)