如何添加與選項卡活動不同的活動,例如我有許多選項卡,如家,添加音樂,添加視頻等,主頁選項卡附加與家庭活動。在家庭活動中有一個按鈕,單擊此按鈕頁面後將轉到另一個活動,那麼我如何才能看到此活動的選項卡。添加子活動與選項卡活動
回答
正如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教程,使一切正常工作
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
雖然這個鏈接可能回答問題,最好在這裏包含答案的重要部分,並提供參考鏈接。如果外部鏈接頁面在將來 – Panther
更改,我已經完成了佈局,那麼僅限鏈接的答案將不會有任何用處,所以請建議我如何在沒有片段的情況下做到這一點 –
- 1. 爲選項卡式活動添加了新活動
- 2. 如何在選項卡活動中創建子活動
- 3. 離子選項卡活動顏色
- 4. 活動鏈接選項卡
- 5. 選項卡活動崩潰
- 6. Android選項卡活動
- 7. Javascript選項卡 - 活動類
- 8. 如何將選項卡式活動添加到導航抽屜活動中?
- 9. 如何在選項卡式活動的每個選項卡中添加不同的活動性
- 10. 使用CSS指向非活動選項卡的活動選項卡?
- 11. Jquery移動選項卡不活動
- 12. 動態android選項卡活動
- 13. 選項卡視圖活動滾動
- 14. API 21滑動選項卡活動
- 15. 如何從android中的選項卡活動啓動fragement活動?
- 16. 使用選項卡式活動刷卡
- 17. 動態更改活動選項卡索引jQuery UI選項卡
- 18. Bootstrap選項卡:添加/刪除活動選項卡內的span範圍
- 19. 選擇活動選項卡按日期
- 20. 如何從其他活動添加片段選項卡
- 21. 如何將片段添加到選項卡式活動?
- 22. 在選項卡式活動中添加橫幅
- 23. bootstrap選項卡不會將活動類添加到鏈接
- 24. 如何在選項卡式活動android中添加web視圖?
- 25. 如何在選項卡活動中添加Listview?
- 26. 如何將地圖添加到選項卡式活動(Android)
- 27. 選項卡:目標僞添加無效Javascript的活動樣式
- 28. 將活動類添加到多個選項卡導航
- 29. 如何將活動添加到選項卡?
- 30. 在選項卡式活動中添加和替換片段
,如果我沒有做的片段,所以我該怎麼辦..... –