问题
I am using Android's SlidingTabColors sample in my application layout. I have three tabs initialized. Due to the default tab layout all the tabs have same layout. I have searched everything about setCustomTabView; but unable to get it implemented. I wanted to know where should i call this method? What statements are to be used? I have used a switch statement in the onViewCreated method of the ContentFragment class. The code I have written in the onViewCreated method is posted below:
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
TextView title = (TextView) view.findViewById(R.id.item_title);
int indicatorColor = args.getInt(KEY_INDICATOR_COLOR);
switch (indicatorColor)
{
case Color.RED:
{
SlidingTabLayout mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setCustomTabView(R.layout.speed_test, R.id.hello1);
}
case Color.GREEN:
title.setText("Subject: " + args.getCharSequence(KEY_TITLE));
case Color.BLUE:
title.setText("Header: " + args.getCharSequence(KEY_TITLE));
}
}
The speed_test.xml file is posted below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello"
android:id="@+id/hello1"
/>
</RelativeLayout>
Edited:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SlidingTabLayout.setCustomTabView(R.layout.speed_test,R.id.hello1);
if (savedInstanceState == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
SlidingTabsColorsFragment fragment = new SlidingTabsColorsFragment();
transaction.replace(R.id.sample_content_fragment, fragment);
transaction.commit();
}}
The error I am getting:
error:(139, 25) error: non-static method setcustomtabview(int,int) cannot be referenced from a static context
回答1:
First thing is to look at the original source code from google. You can see that there is a method like this:
public void setCustomTabView(int layoutResId, int textViewId) {
mTabViewLayoutId = layoutResId;
mTabViewTextViewId = textViewId;
}
When you take a look at the method populateTabStrip() you can see that there is inflating a custom view (which you have set) when the id isn't 0
...
if (mTabViewLayoutId != 0) {
// If there is a custom tab view layout id set, try and inflate it
tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip,
false);
tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
}
...
That mean that you must call the setCustomTabView() with the Layout as first parameter and the TextView (which will displayed the text) as second parameter. Like
mSlidingTabLayout.setCustomTabView(R.layout.custom_x, R.id.item);
Don't set the layout to 0 (zero). The View will automaticly use the default TabView (which is a single TextView).
The layout, in my example, look like this and is called custom_x.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_height="wrap_content" />
</LinearLayout>
Important! The method setViewPager() must called after setCustomTabView() in your Activity/Fragment.
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setCustomTabView(R.layout.custom_x, R.id.item);
mSlidingTabLayout.setViewPager(mViewPager);
Then you get a result like this:
It's also as gist available.
回答2:
Inside SlidingTabLayout the tabs layout is governed by mTabViewLayoutId and mTabViewTextViewId
just add a method:
/**
* Set the custom layout to be inflated for the tab views.
*
* @param layoutResId Layout id to be inflated
* @param textViewId id of the {@link TextView} in the inflated view
*/
public void setCustomTabView (int layoutResId, int textViewId) {
mTabViewLayoutId = layoutResId;
mTabViewTextViewId = textViewId;
}
and you are set to go. you should call this method @OnCreate of your activity, you can pass 0 as the first parameter for not to include Layout, and just pass the second param as for the layout id of some TextView. notice that you don't need to inlate the Views as you did
@Layout/my_text_view
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello"
android:id="@+id/hello1"
/>
@onCreateView
@Override
public void onCreate(Bundle savedInstanceState){
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(new SectionsPagerAdapter(getFragmentManager()));
tabImageView = (ImageView) findViewById(R.id.tab_image_view);
// Create the mAdapter that will return a fragment for each of the three
// primary sections of the activity.
slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
slidingTabLayout.setDividerColors(getResources().getColor(android.R.color.transparent));
slidingTabLayout.setSelectedIndicatorColors(getResources().getColor(android.R.color.white));
slidingTabLayout.setCustomTabView(0, R.layout.my_text_view);
}
来源:https://stackoverflow.com/questions/27676844/how-can-custom-tab-view-be-implemented-using-setcustomtabview-method-in-slidingt