2016-07-27 14 views
-2

Ich versuche, eine benutzerdefinierte Registerkarte Aktivität zu erstellen, aber ich fand Standard-Registerkarte Aktivität. In dem ich die Indikatorfarbe auf rot ändern möchte, passiert leider nichts. Bitte Hilfe und Danke im Voraus.Wie erstelle ich eine benutzerdefinierte Tab-Aktivität in Android?

+0

Post-Code, was Sie versucht haben. – Masum

+0

'TabActivity Veraltet'. Zu alte Frage. Was Sie bisher versucht haben –

+0

können Sie folgen - http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/ –

Antwort

0

Es gibt zwei Klassen:

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

Innen 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"); 
Verwandte Themen