2017-04-18 29 views
0

我使用选项卡式页面制作应用程序。[Xamarin]风格,Theme.Holo&Theme.AppCompat

如果我使用AppCompat,我无法自定义选项卡,但我可以使用 移动选项卡进行滑动操作。

如果我使用Holo,我可以自定义选项卡,但我不能使用 滑动动作的移动选项卡。

我霍洛主题

 <resources> 
     <!--my custom theme--> 
     <style name="MyTheme" 
       parent="@android:style/Theme.Holo"> 
      <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item> 
      <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item> 
     <item name="android:background">#FFFFFF</item>  
     </style> 

     <!--tab text style--> 
     <style name="MyActionBarTabText" 
       parent="@android:style/Widget.Holo.ActionBar.TabText"> 
      <item name="android:textColor">#000000</item> 
      <item name="android:background">@drawable/tab_selector</item>  
     </style> 


     <!--tab style--> 
     <style name="MyActionBarTabStyle" parent="@android:style/Widget.Holo.ActionBar.TabView"> 
      <!--<item name="android:background">#DCEBF0</item>--> 
      <item name="android:background">@drawable/tab_selector</item> 
      <item name="android:layout_width">1dp</item> 
     </style> 
     </resources> 

我的程序兼容性主题

 <resources> 
      <style name="MyTheme" parent="MyTheme.Base"> 
      </style> 
      <!-- Base theme applied no matter what API --> 
      <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"> 
       <!--If you are using revision 22.1 please use just windowNoTitle. Without android:--> 
       <item name="windowNoTitle">true</item> 
       <!--We will be using the toolbar so no need to show ActionBar--> 
       <item name="windowActionBar">false</item> 
       <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette--> 


      <!-- You can also set colorControlNormal, colorControlActivated 
       colorControlHighlight and colorSwitchThumbNormal. --> 
       <item name="windowActionModeOverlay">true</item>  
      </style> 


     </resources> 

如何自定义选项卡,并使用刷卡的动作?

或者我可以只更改选项卡主题全息在AppCompat?

回答

0

使用程序兼容性,你仍然可以做到这一点编程:

使用此代码在您的OnCreate覆盖方法(活动要显示的分页):

//Setting TabLayout 
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
    tabLayout.addTab(tabLayout.newTab()); 
    tabLayout.addTab(tabLayout.newTab()); 
    tabLayout.addTab(tabLayout.newTab()); 
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

    //Setting viewPager 
    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); 
    final PagerAdapter adapter = new 
           PagerAdapter(getSupportFragmentManager(), 
           tabLayout.getTabCount()); 
    viewPager.setAdapter(adapter); 
    viewPager.addOnPageChangeListener(new 
        TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
    tabLayout.setupWithViewPager(viewPager); 

,并创建一个类你页面适配器这样的:

public class PagerAdapter extends FragmentStatePagerAdapter { 
    int mNumOfTabs; 

    private String[] tabTitles = new String[]{"TAB1", "TAB2", "TAB3"}; 


    public PagerAdapter(FragmentManager fm, int NumOfTabs) { 
     super(fm); 
     this.mNumOfTabs = NumOfTabs; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return tabTitles[position]; 
    } 

    @Override 
    public Fragment getItem(int position) { 

    switch (position) { 
     case 0: 
      Tab1 tab1 = new Tab1(); 
      return tab1; 
     case 1: 
      Tab2 tab2 = new Tab2(); 
      return tab2; 
     case 2: 
      Tab3 tab3 = new Tab3(); 
      return tab3; 
     default: 
      return null; 
    } 
} 

@Override 
public int getCount() { 
    return mNumOfTabs; 
} 
} 

这是你如何在你的XML布局文件中定义一个ViewPager:

<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@id/tab_layout" 
    android:background="@drawable/gradient_grey"> 

让我知道它是否适合您!