【问题标题】:How to change the style of tabs on android 2.3- actionbarsherlock如何更改 android 2.3-actionbarsherlock 上的标签样式
【发布时间】:2013-06-02 15:54:31
【问题描述】:

您好,我想更改 android

我还想修改它的背景和图标。

我正在使用actiobarsherlock 库。

这是我尝试过的

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="?attr/actionBarSize"
            android:layout_weight="0"
            style="@style/ActionBarTabStyle.Example"
            android:orientation="horizontal"
            android:tabStripEnabled="false" />

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

        <com.mainapp.CustomViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

</TabHost>

风格

<style name="ActionBarTabStyle.Example" parent="@style/Widget.Sherlock.Light.ActionBar.TabView">
        <item name="android:background">@drawable/tab_indicator_ab_example</item>
    </style>

-

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

    <!-- Pressed -->
    <!--    Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_example" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_example" />

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

谢谢!

【问题讨论】:

  • 你用的什么风格?
  • 您好,我更新了问题。
  • 而选择器中使用的drawable是?
  • jgilfelt.github.io/android-actionbarstylegenerator 我使用这个网站来生成这些drawable。但是,如果我从 TabHost 中删除 style="@style/ActionBarTabStyle.Example" 行,那么似乎什么都没有改变。
  • 如果删除样式不会改变事情,那么其他东西会弄乱标签。我觉得奇怪的一件事是你为什么使用android:layout_marginTop="?attr/actionBarSize" 属性?

标签: android actionbarsherlock android-tabs


【解决方案1】:

更新:

你使用 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 我所有的三个片段都使用 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);所以标签 不显示。我试图同时显示选项卡和列表导航,但现在 在职的。我想在我的 碎片。我怎样才能实现它? – 约翰·史密斯 1 小时前

你不能两者兼得。在这种情况下我会推荐的是

a) 您可以开始使用抽屉式导航/滑动菜单模式,而不是使用 Navigation_mode_list。

https://developer.android.com/design/patterns/navigation-drawer.html

b) 使用 viewpager + viewpager 条代替选项卡。然后就可以使用 Naviagation_mode_list 了。

c) 代替使用navigation_mode_list,您可以有一个操作栏菜单项,有人可以按下它来启动一个对话框。

原创

ActionBar 中的选项卡和 Tabhost 中的选项卡实际上是不同的,并且不使用相同的样式集。这可以解释为什么你改变风格没有做任何事情。

现在,您可以采取几种方法来获得类似 Holo 的风格。

  • 您可以使用 ActionBarSherlock.Tabs + Fragments 而根本不使用 TabHost。 (我的建议)
  • 您可以直接使用Holo Colors Generator 设置 Tabhost 的样式(实际上特定的 TabHost 需要更多的努力)

我重新开始 (ActionBarSherlock.Tabs + Fragments) 的原因是 Android 开发的发展方向。使用 Fragments 可以让您拥有完全可重用的代码块。此外,viewpager 还可以让您进行流畅的滑动操作以添加交互性。这是实现它并在 2.2 模拟器上测试的代码。

package com.example.stackoverflow;

import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;

public class MainActivity extends SherlockFragmentActivity implements
    TabListener {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
 * will keep every loaded fragment in memory. If this becomes too memory
 * intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

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

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }
}

@Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return "Simple";
        case 1:
            return "Contacts";
        case 2:
            return "Custom";
        case 3:
            return "Throttle";
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy,
                container, false);
        TextView dummyTextView = (TextView) rootView
                .findViewById(R.id.section_label);
        dummyTextView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
        return rootView;
    }
}

 }

为了生成这段代码,我使用了 File->New-> Other -> Android Folder -> Android - Activity -> Blank Activity -> Navigation style 设置为 Fixed Tabs + swipe。但是,您不能直接将其与 actionbarsherlock 一起使用,因为您需要将 Actionbar 变量替换为 getSupportActionBar ,就像我在上面的代码中所做的那样。

【讨论】:

  • 你使用actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 我所有的三个片段都使用actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); 所以标签不显示。我试图同时显示选项卡和列表导航,但现在可以工作了。我想在我的片段中使用标签导航和导航模式列表。我怎样才能实现它?
  • 我更新了我的答案。这与我提交答案的第一天之间有相当长的差距。哈哈
  • 您好杰拉德,感谢您的建议。这是我在 Play 商店中的应用程序 play.google.com/store/apps/details?id=com.screen.callerpro 这些标签看起来不太好。我正在努力更改应用程序的整个 UI。我不能使用滑动抽屉或对话框菜单。可能是查看器条适合我的需要,但这需要很多时间。
猜你喜欢
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多