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