0

如何添加與選項卡活動不同的活動,例如我有許多選項卡,如家,添加音樂,添加視頻等,主頁選項卡附加與家庭活動。在家庭活動中有一個按鈕,單擊此按鈕頁面後將轉到另一個活動,那麼我如何才能看到此活動的選項卡。添加子活動與選項卡活動

回答

1

正如Anrijs說TabActivity已被棄用,在Android所以用支護設計庫的TabLayout

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@id/tab_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:tabGravity="fill" 
    app:tabMode="fixed" 
    app:tabTextAppearance="@style/TabTextStyle" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

然後創建一個ViewPager

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

然後實現爲每個活動的Fragment

並按照this教程,使一切正常工作

+0

,如果我沒有做的片段,所以我該怎麼辦..... –

0

TabActivity已在API級別13中棄用。您應該考慮使用ViewPager和TabLayout。

添加設計支持庫項目的build.gradle:

dependencies { 
    compile 'com.android.support:design:22.2.0' 
} 

添加TabLayout和ViewPager你

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

<android.support.design.widget.TabLayout 
    android:id="@+id/sliding_tabs" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:tabMode="scrollable" /> 

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

創建片段:

// In this case, the fragment displays simple text based on the page 
public class PageFragment extends Fragment { 
    public static final String ARG_PAGE = "ARG_PAGE"; 

    private int mPage; 

    public static PageFragment newInstance(int page) { 
     Bundle args = new Bundle(); 
     args.putInt(ARG_PAGE, page); 
     PageFragment fragment = new PageFragment(); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mPage = getArguments().getInt(ARG_PAGE); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_page, container, false); 
     TextView textView = (TextView) view; 
     textView.setText("Fragment #" + mPage); 
     return view; 
    } 
} 

的Cre吃FragmentPageAdapter:

public class SampleFragmentPagerAdapter extends FragmentPagerAdapter { 
    final int PAGE_COUNT = 3; 
    private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" }; 
    private Context context; 

    public SampleFragmentPagerAdapter(FragmentManager fm, Context context) { 
     super(fm); 
     this.context = context; 
    } 

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

    @Override 
    public Fragment getItem(int position) { 
     return PageFragment.newInstance(position + 1); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     // Generate title based on item position 
     return tabTitles[position]; 
    } 
} 

設定活動:

public class MainActivity extends FragmentActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Get the ViewPager and set it's PagerAdapter so that it can display items 
     ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); 
     viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(), 
     MainActivity.this)); 

     // Give the TabLayout the ViewPager 
     TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs); 
     tabLayout.setupWithViewPager(viewPager); 
    } 
} 

來源:https://guides.codepath.com/android/Google-Play-Style-Tabs-using-TabLayout

+0

雖然這個鏈接可能回答問題,最好在這裏包含答案的重要部分,並提供參考鏈接。如果外部鏈接頁面在將來 – Panther

+0

更改,我已經完成了佈局,那麼僅限鏈接的答案將不會有任何用處,所以請建議我如何在沒有片段的情況下做到這一點 –