繁体   English   中英

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

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

我使用Sherlock Fragment创建了一个类,以使用ViewPager启用“滑动”视图。 现在,我正在尝试使用setBackgroundResource()方法更改tabspec中的标签背景,但是它不起作用。 我想为每个标签添加自定义背景,同时将其聚焦/选中/未选中。

我尝试使用选择器,但没有用

<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)); 

这是我的主班

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);

    }

}

这是基于我刚刚所做的事情(以及我当时的痛苦)。 我没有尝试编译它,但希望能有所帮助。 祝好运!

在主要活动的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

<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>

基本上,我们通过将TextView放在布局中并添加背景来创建“假”标签。

另外要注意的是,您的可绘制对象需要为选定的和未选定的可绘制对象都包括一个选择器。 如果您需要一个示例,请查看Android Asset Studio生成的内容,这就是我用来制作标签的内容。

根据需要,对任意多个标签重复此操作。

如果其中任何部分含糊或令人困惑,请随时提出问题。 ;)

暂无
暂无

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

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