10

我已閱讀此鏈接中的信息:Use Tab with new ToolBar (AppCompat v7-21)並做了大量相同的研究。在Android開發人員工具中使用Support Library v7:21添加滑動選項卡現有項目

但問題是SlidingTabLayout項目正在使用Gradle構建文件和結構。我想使用eclipse添加標籤佈局。我該怎麼做呢?

雖然我在Android Studio中運行了SlidingTabLayout項目。但是,如何在V7:21中將選項卡添加到工具欄?

注: 這一切都需要在現有的項目,它擁有一切設置和API警告我棄用API像ActionBar.setNavigationMode等21

回答

0

只需添加ViewPager和SlidingTabLayout到做你的XML:

<com.example.SlidingTabLayout 
      android:id="@+id/sliding_tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 

    <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

在代碼爲您ViewPager(如實施例FragmentPagerAdapter)的適配器和它們綁定到您的ViewPager,所述SlidingTabLayout後綁定到ViewPager:

mViewPager = (ViewPager) findViewById(R.id.pager); 
mViewPager.setAdapter(mSectionsPagerAdapter); 

final SlidingTabLayout tabLayout = (SlidingTabLayout)findViewById(R.id.sliding_tabs); 
tabLayout.setViewPager(mViewPager); 
15

1。從https://developer.android.com/samples/SlidingTabsColors/src/com.example.android.common/view/SlidingTabLayout.html複製SlidingTabLayout.java並將其粘貼到您的包中。

MainActivity.java

package com.example.mysliding; 

import com.example.android.common.view.SlidingTabLayout; 
import com.example.mysliding.SlidingTabsBasicFragment.SamplePagerAdapter; 

import android.support.v4.app.FragmentTransaction; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.widget.Toolbar; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class MainActivity extends ActionBarActivity { 

static final String LOG_TAG = "SlidingTabsBasicFragment"; 
private SlidingTabLayout mSlidingTabLayout; 
private ViewPager mViewPager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragment_sample); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); 

    mViewPager = (ViewPager) findViewById(R.id.viewpager); 
    mViewPager.setAdapter(new SamplePagerAdapter()); 
    mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs); 
    mSlidingTabLayout.setViewPager(mViewPager); 

    /* 
    * FragmentTransaction transaction = 
    * getSupportFragmentManager().beginTransaction(); 
    * SlidingTabsBasicFragment fragment = new SlidingTabsBasicFragment(); 
    * transaction.replace(R.id.sample_content_fragment, fragment); 
    * transaction.commit(); 
    */ 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

class SamplePagerAdapter extends PagerAdapter { 

    /** 
    * @return the number of pages to display 
    */ 
    @Override 
    public int getCount() { 
     return 5; 
    } 

    /** 
    * @return true if the value returned from 
    *   {@link #instantiateItem(ViewGroup, int)} is the same object 
    *   as the {@link View} added to the {@link ViewPager}. 
    */ 
    @Override 
    public boolean isViewFromObject(View view, Object o) { 
     return o == view; 
    } 

    // BEGIN_INCLUDE (pageradapter_getpagetitle) 
    /** 
    * Return the title of the item at {@code position}. This is important 
    * as what this method returns is what is displayed in the 
    * {@link SlidingTabLayout}. 
    * <p> 
    * Here we construct one using the position value, but for real 
    * application the title should refer to the item's contents. 
    */ 
    @Override 
    public CharSequence getPageTitle(int position) { 
     return "Item " + (position + 1); 
    } 

    // END_INCLUDE (pageradapter_getpagetitle) 

    /** 
    * Instantiate the {@link View} which should be displayed at 
    * {@code position}. Here we inflate a layout from the apps resources 
    * and then change the text view to signify the position. 
    */ 
    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     // Inflate a new layout from our resources 

     View view = getLayoutInflater().inflate(R.layout.pager_item, 
       container, false); 
     // Add the newly created View to the ViewPager 
     container.addView(view); 

     // Retrieve a TextView from the inflated View, and update it's text 
     TextView title = (TextView) view.findViewById(R.id.item_title); 
     title.setText(String.valueOf(position + 1)); 

     Log.i(LOG_TAG, "instantiateItem() [position: " + position + "]"); 

     // Return the View 
     return view; 
    } 

    /** 
    * Destroy the item from the {@link ViewPager}. In our case this is 
    * simply removing the {@link View}. 
    */ 
    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 
     container.removeView((View) object); 
     Log.i(LOG_TAG, "destroyItem() [position: " + position + "]"); 
    } 

} 

}

fragment_sample.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <android.support.v7.widget.Toolbar 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/my_awesome_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:minHeight="?attr/actionBarSize" 

     app:theme="@style/ThemeOverlay.AppCompat.ActionBar"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <com.example.android.common.view.SlidingTabLayout 
       android:id="@+id/sliding_tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 
    </android.support.v7.widget.Toolbar> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="0px" 
     android:layout_weight="1" 
     android:background="@android:color/white" /> 

</LinearLayout> 

Pager_item.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:gravity="center"> 

    <TextView 
      android:id="@+id/item_subtitle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Page:"/> 

    <TextView 
      android:id="@+id/item_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="80sp" /> 

</LinearLayout> 
+0

上述工作爲我添加選項卡到toolbar.Is它工作在你的情況。 – 2014-11-13 04:45:35

+0

即時通訊錯誤11-21 12:34:15.480:E/AndroidRuntime(17172):引起:java.lang.ClassNotFoundException:在路徑中找不到類「com.example.toolbartabdemo.view.SlidingTabLayout」 DexPathList [[zip文件「/data/app/com.example.toolbartabdemo-1/base.apk"],nativeLibraryDirectories=[/vendor/lib,/system/lib]] – jyomin 2014-11-21 07:06:15

+0

我得到它的工作,通過在訂單中檢查v5和出口選項卡,並非常感謝你分享它的代碼完美的工作,節省了我很多時間。 – jyomin 2014-11-21 07:26:13

3

我也有同樣的問題。由於Android一次又一次地改變了ActionBar和Tabs的使用,所以很難知道什麼是最合適的實現。

所以我遵循示例that is shown here,但我添加了一些額外的功能。你可以看到我的版本和它的使用on this gist

我加了什麼;

  • 啓用給標籤寬度相等(如果有兩個標籤,他們就會有動作條的半寬)
  • 顯示圖像而不是文本或顯示在標籤
  • 兼而有之(自定義視圖)文本和圖像

UPDATE

有一個名爲Tab Layout支持庫中的新佈局。你可以用它來代替。

此外,你可以看到另一個迴應here告訴更多關於它。

+0

我實現了該解決方案,但它遠離材料設計[選項卡](http://www.google.com/design/spec/components/tabs.html)。我仍在尋找解決方案:/ – gkiko 2015-01-05 16:26:42

+0

實際上取決於您對選項卡進行設計。我更改了SlidingTabStrip類中的常量,並向activity_main.xml添加了完全相同的顏色。你可以再次檢查[gist](https://gist.github.com/eluleci/199be9a0d1af42653b5b)。所以現在看起來和你給的鏈接中的材料標籤完全一樣。提示:在您的應用中使用帶有黑暗主題的appcompat 7。 – eluleci 2015-01-06 10:21:39

+2

重要提示:此答案現在已被棄用,下面是正確的答案:http://stackoverflow.com/questions/26540078/use-tab-with-new-toolbar-appcompat-v7-21/26543020#26543020 – 2015-07-26 22:03:33

相關問題