[英]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.