2016-12-14 44 views
1

我試圖實現將在我的項目的活動之一中帶來片段的選項卡。問題是標籤在不同的設備上佔據不同的寬度尺寸。獲取不同寬度的ActionBar選項卡

實施例(關係7):突片不佔用整個寬度nexus 7

實施例2(星系S5):突出部在水平方向上可滾動,所以它們佔據比所述動作條的整個寬度更enter image description here

SecondActivity.java:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_second); 

    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mActionBar = getActionBar(); 
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), mDate); 

    mAdapter.setCurrentIndex(mSelectedTab); 

    mViewPager.setAdapter(mAdapter); 
    mActionBar.setDisplayShowHomeEnabled(false); 
    mActionBar.setDisplayShowTitleEnabled(false); 
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME); 
    mViewPager.setPagingEnabled(false); 

    // Adding Tabs 
    ActionBar.Tab firstTab = mActionBar 
      .newTab() 
      .setIcon(R.drawable.first_selector) 
      .setTabListener(this); 

    ActionBar.Tab secondTab = mActionBar 
      .newTab() 
      .setIcon(R.drawable.second_selector) 
      .setTabListener(this); 

    ActionBar.Tab thirdTab = mActionBar 
      .newTab() 
      .setIcon(R.drawable.third_selector) 
      .setTabListener(this); 

    mActionBar.addTab(firstTab); 
    mActionBar.addTab(secondTab); 
    mActionBar.addTab(thirdTab); 
    switch (mSelectedTab) { 
     case 0: 
      mActionBar.selectTab(firstTab); 
      break; 
     case 1: 
      mActionBar.selectTab(secondTab); 
      break; 
     case 2: 
      mActionBar.selectTab(thirdTab); 
      break; 
    } 

    mViewPager.setOffscreenPageLimit(3); 

    LayoutInflater mInflater = (LayoutInflater) this 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View mCustomView = mInflater.inflate(R.layout.my_layout_header, null); 
    mActionBar.setCustomView(mCustomView); 
    mActionBar.setDisplayShowCustomEnabled(true); 
} 

activity_second.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main_messages_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:focusable="true" 
    android:focusableInTouchMode="true"> 

    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"></android.support.v4.view.ViewPager> 
</RelativeLayout> 

TabsPagerAdapter.java:

public class TabsPagerAdapter extends FragmentStatePagerAdapter { 
    private String mDate; 
    private FragmentManager mFragmentManager; 
    private int mCurrentIndex; 
    SparseArray<Fragment> registeredFragments; 

    public TabsPagerAdapter(FragmentManager fm, String date) { 
     super(fm); 
     mFragmentManager = fm; 
     this.mDate = date; 
     initializeFragments(); 
    } 

    private void initializeFragments(){ 
     registeredFragments = new SparseArray<Fragment>(); 

     FirstFragment firstFragment = FirstFragment.newInstance(mDate); 
     registeredFragments.put(0, firstFragment); 
     SecondFragment secondFragment = SecondFragment.newInstance(mDate); 
     registeredFragments.put(1, secondFragment); 
     ThirdFragment thirdFragment = ThirdFragment.newInstance(mDate); 
     registeredFragments.put(2, thirdFragment); 
    } 

    @Override 
    public Fragment getItem(int index) { 
     return registeredFragments.get(index); 
    } 

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

    @Override 
    public int getItemPosition(Object object) { 
     return POSITION_NONE; 
    } 

    public int getCurrentIndex() { 
     return mCurrentIndex; 
    } 

    public void setCurrentIndex(int currentIndex) { 
     this.mCurrentIndex = currentIndex; 
    } 
} 

爲FirstFragment.java和layout_first_fragment.xml的代碼,以及用於在第二和第三片段唐沒關係。然而,這裏是該模塊的的build.gradle:

buildscript { 
    repositories { 
     maven { url 'https://maven.fabric.io/public' } 
    } 

    dependencies { 
     classpath 'io.fabric.tools:gradle:1.+' 
    } 
} 
apply plugin: 'com.android.application' 

repositories { 
    maven { url 'https://maven.fabric.io/public' } 
} 


//Put Fabric plugin after Android plugin 
apply plugin: 'io.fabric' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "23.0.2" 

    defaultConfig { 
     applicationId "com.myapplication.android" 
     minSdkVersion 11 
     targetSdkVersion 23 
     versionCode 9 
     versionName "1.3.0" 

     multiDexEnabled true 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    useLibrary 'org.apache.http.legacy' 
    dexOptions{ 
     javaMaxHeapSize "4g" 
    } 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 

    compile "com.android.support:appcompat-v7:25.0.+" 
    compile 'com.google.android.gms:play-services:10.0.1' 
    compile 'it.sephiroth.android.library.horizontallistview:hlistview:1.2.2' 
    compile 'com.anjlab.android.iab.v3:library:[email protected]' 
    compile project(':libraries:SlidingMenu') 
    compile project(':PullToRefresh') 
    compile('com.crashlytics.sdk.android:crashlytics:[email protected]') { 
     transitive = true; 
    } 
    compile 'com.android.support:multidex:1.0.1' 
    compile 'com.android.support:design:25.0.1' 
} 

我想getActionBar()初始化動作條是設備的整個寬度。我試圖創建一個自定義操作欄,但我有同樣的問題。

回答

0

ActionBar選項卡現在已正式被棄用。現在應該使用TabLayout來構建選項卡。 此鏈接可能會對您有幫助 https://github.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-TabLayout

+0

感謝您的回覆。即使ActionBar選項卡已被棄用,我仍然想使用它來解決問題,因爲它是一個大項目,並且重新構建一切都是一件痛苦的事情。在這種情況下可以做些什麼? –

+0

你可能想檢查這個http://stackoverflow.com/questions/14979327/stacked-actionbar-tab-bar-not-filling-parent。 – pipeur10