-2
我正尝试创建自定义选项卡活动,但我找到了默认选项卡活动。 因为我想改变指示器的颜色为红色,但不幸的是没有任何反应。 请帮助并提前致谢。如何在Android中创建自定义选项卡活动?
我正尝试创建自定义选项卡活动,但我找到了默认选项卡活动。 因为我想改变指示器的颜色为红色,但不幸的是没有任何反应。 请帮助并提前致谢。如何在Android中创建自定义选项卡活动?
有2类:
MyTabs.java
public class MyTabs extends TabLayout {
String text;
public MyTabs(Context context) {
super(context);
}
public MyTabs(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTabs(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setText(String text) {
this.text = text;
}
@Override
public void setTabTextColors(int normalColor, int selectedColor) {
super.setTabTextColors(normalColor, selectedColor);
}
@Override
public void setBackgroundColor(int color) {
super.setBackgroundColor(color);
}
@Override
public void addTab(@NonNull Tab tab) {
super.addTab(tab);
}
@NonNull
@Override
public Tab newTab() {
return super.newTab();
}
@Override
public void setSmoothScrollingEnabled(boolean smoothScrollingEnabled) {
super.setSmoothScrollingEnabled(smoothScrollingEnabled);
}
@Override
public void setTabMode(int mode) {
super.setTabMode(mode);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<com.mypackage.here.MyTabs
android:id="@+id/mytabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
内MainActivity.java
ViewPager mViewPager;
MyTabs tabs;
TabLayout.Tab latestTab;
TabLayout.Tab allVideosTab;
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
//Tab layout
tabs = (MyTabs) findViewById(R.id.mytabs);
tabs.setTabTextColors(Color.LTGRAY, Color.WHITE);
tabs.setTabGravity(TabLayout.GRAVITY_CENTER);
tabs.setSmoothScrollingEnabled(true);
tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
latestTab = tabs.newTab();
allVideosTab = tabs.newTab();
//ADD TABS IN REVERSE ORDER OF APPEARANCE
tabs.addTab(allVideosTab);
tabs.addTab(latestTab);
//*********ADD ALL tabs above this*/
tabs.setupWithViewPager(mViewPager);
//Manipulate tabs here
latestTab.setText("Latest");
allVideosTab.setText("All Videos");
发布您的代码你都试过了。 – Masum
'TabActivity Deprecated'。太旧的问题。你到目前为止所尝试的 –
你可以按照这个 - http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/ –