Я где-то читал, что вкладка в студии Android имеет некоторый устаревший код. Так, я должен использовать это или нет? И есть ли другие новые способы добиться того же?Tabbed Activity в Android Studio
0
A
ответ
0
Почему вы не попробуете конструкцию материалов с раздвижными вкладками? Они обеспечивают лучший обзор и интерактивный интерфейс.
Шаг 1: скачать эти файлы скопировать, как в Android Studio:
Шаг 2: в color.xml установить цветовую схему в соответствии с вашими требованиями:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ColorPrimary">#e62117</color>
<color name="ColorPrimaryDark">#c31c13</color>
<color name="tabsScrollColor">#8a140e</color>
</resources>
Шаг 3: Создание toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@color/ColorPrimary"
android:elevation="2dp"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
xmlns:android="http://schemas.android.com/apk/res/android" />
Шаг 4: в activity_main.xml поместить следующий код:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<com.example.slidingtab.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="@color/ColorPrimary"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
/>
Шаг 5: Теперь создавать макеты для вкладок в tab_1.xml
, tab_2.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="You Are In Tab 1"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
Шаг 6: Аналогично создать два фрагмента, как fragment_one .java и fragment_two.java
public class Tab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1,container,false);
return v;
}
}
Шаг 7: ViewPager класс:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
}
//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
if(position == 0) // if the position is 0 we are returning the First tab
{
Tab1 tab1 = new Tab1();
return tab1;
}
else // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
{
Tab2 tab2 = new Tab2();
return tab2;
}
}
// This method return the titles for the Tabs in the Tab Strip
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
return NumbOfTabs;
}
}
Шаг 8: Заключительный шаг:
public class MainActivity extends ActionBarActivity {
// Declaring Your View and Variables
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Home","Events"};
int Numboftabs =2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}
}
Смежные вопросы
- 1. Android Tabbed Activity Scrolling
- 2. android studio tabbed activity button to new activity
- 3. Tabbed Activity template Android 2.1.2
- 4. Android Tabbed Activity, overthinking it?
- 5. onClickListener не работает в Tabbed Activity Android
- 6. Android Studio по умолчанию «Tabbed Activity», как прокручивать фрагменты?
- 7. Как установить фрагменты в Android Tabbed Activity
- 8. Как использовать scrollView в Tabbed Activity - Android?
- 9. Добавить баннер в tabbed activity
- 10. swipe using Tabbed activity
- 11. Footer in Tabbed Activity
- 12. Android Tabbed Activity on clickbutton не работает
- 13. Android Tabbed Activity Bottom off Screen
- 14. Android Tabbed Activity Добавить новый фрагмент
- 15. Android Tabbed Activity Добавить новый фрагмент volley
- 16. Внедрение OnFragmentInteractionListener в Tabbed Activity с ViewPager
- 17. Могу ли я заменить ActionBarActivity на AppCompatActivity в Tabbed Activity в Android Studio?
- 18. Как показать виды раздутого макета в Tabbed Activity?
- 19. Android Tabbed Application
- 20. В Tabbed Activity с ViewPager, ListView появляется снизу снизу
- 21. Android Tabbed Layout setContent
- 22. Android tabbed layout/view crashing
- 23. Android tabbed Активность
- 24. Android - Tabbed Fragment
- 25. Tabbed Toolbar в Android L
- 26. Android studio blank activity throws error
- 27. Android Studio: Theme Change - Empty Activity
- 28. Android Studio - 2 Spinners In One Activity
- 29. Android Studio Camera Use Activity не найден
- 30. Навигационный ящик Intent Activity Android Studio
Смотрите 'TabLayout' –
Спасибо @ReazMurshed –
Смотрите эту документацию реализации. http://stackoverflow.com/documentation/android/124/material-design-for-all-android-versions/11500/tablayout#t=201608221113457391885 –