简体   繁体   English

Android中带有Sherlock Fragment的操作栏选项卡的自定义背景

[英]Custom background for Action bar tabs with Sherlock Fragment in Android

I've created a class with Sherlock Fragment to enable the Swipe view with ViewPager. 我使用Sherlock Fragment创建了一个类,以使用ViewPager启用“滑动”视图。 Now I'm trying to change the tab background which was possible in tabspec using setBackgroundResource() method but It's not working. 现在,我正在尝试使用setBackgroundResource()方法更改tabspec中的标签背景,但是它不起作用。 I would like to add custom background for each tabs while its being focused/selected/not selected. 我想为每个标签添加自定义背景,同时将其聚焦/选中/未选中。

I tried using a selector but It didn't worked 我尝试使用选择器,但没有用

<item android:drawable="@drawable/selected" android:state_selected="true"/>
<item android:drawable="@drawable/not_selected" android:state_selected="false"/>

mActionBar.setStackedBackgroundDrawable(getResources().getDrawable(
                R.drawable.tab_indicator)); 

Here is my main class 这是我的主班

public class MainActivity extends SherlockFragmentActivity {

    // Declare Variables
    ActionBar mActionBar;
    ViewPager mPager;
    Tab tab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from activity_main.xml
        setContentView(R.layout.activity_main);

        // Activate Navigation Mode Tabs
        mActionBar = getSupportActionBar();
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Locate ViewPager in activity_main.xml
        mPager = (ViewPager) findViewById(R.id.pager);

        // Activate Fragment Manager
        FragmentManager fm = getSupportFragmentManager();

        // Capture ViewPager page swipes
        ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                // Find the ViewPager Position
                mActionBar.setSelectedNavigationItem(position);
            }
        };

        mPager.setOnPageChangeListener(ViewPagerListener);
        // Locate the adapter class called ViewPagerAdapter.java
        ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
        // Set the View Pager Adapter into ViewPager
        mPager.setAdapter(viewpageradapter);

        // Capture tab button clicks
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {

            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) {
                // Pass the position on tab click to ViewPager
                mPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onTabReselected(Tab tab, FragmentTransaction ft) {
                // TODO Auto-generated method stub
            }
        };

        // Create first Tab
        tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
        mActionBar.addTab(tab);

        // Create second Tab
        tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
        mActionBar.addTab(tab);

        // Create third Tab
        tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
        mActionBar.addTab(tab);

    }

}

This is based on something I just did (and my, what a pain it was). 这是基于我刚刚所做的事情(以及我当时的痛苦)。 I haven't tried compiling this but hopefully it helps. 我没有尝试编译它,但希望能有所帮助。 Good Luck! 祝好运!

In main activity's onCreate method, replace your addTab statements with this: 在主要活动的onCreate方法中,将您的addTab语句替换为:

    ActionBar.Tab tab1 = mActionBar.newTab().setTabListener(tabListener);
    //Create a temporary RelativeLayout and inflate it with your custom layout
    RelativeLayout rl1 = (RelativeLayout) getLayoutInflater().inflate(R.layout.tabLayout, null);

    //Set the title of the tab
    TextView t1 =  (TextView) rl1.findViewById(R.id.tab1_text);
    t1.setText("Tab1 Title");
    t1.setMinimumWidth(150);//Prevents the tabs from becoming too small on larger screens, Change as needed

    //Set the background of the tab to the layout you just created
    tab1.setCustomView(rl1);
    //Add the tab to your ActionBar
    mActionBar.addTab(tab1);

tab1Layout.xml tab1Layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:background="@drawable/yourDrawable"
android:layout_margin="0dp">

<TextView
    android:id="@+id/tab1_text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="@style/yourStyleIfYouWantToUseOne"
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    android:gravity="center|center_horizontal"
    android:textStyle="bold"/>
</RelativeLayout>

Basically, we're creating a "fake" tab by just putting a TextView in a layout and adding a background. 基本上,我们通过将TextView放在布局中并添加背景来创建“假”标签。

As and additional note, your drawable needs to include a selector for both the selected and unselected drawables. 另外要注意的是,您的可绘制对象需要为选定的和未选定的可绘制对象都包括一个选择器。 If you need an example, look at what is generated by the Android Asset Studio , which is what I used to make my tabs. 如果您需要一个示例,请查看Android Asset Studio生成的内容,这就是我用来制作标签的内容。

Repeat this for as many tabs as you need. 根据需要,对任意多个标签重复此操作。

If any part of this is vague or confusing, feel free to ask questions. 如果其中任何部分含糊或令人困惑,请随时提出问题。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM