새 도구 모음과 함께 탭 사용 (AppCompat v7-21)
사용자가 검색보기를 확장 할 때만 탭을 표시하는 탭 및 사용자 지정 ActionBar 테마 ( http://jgilfelt.github.io/android-actionbarstylegenerator/로 생성됨)와 함께 SupportActionBar를 사용하고있었습니다 .
public boolean onMenuItemActionExpand(MenuItem item) {
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
return true;
}
}
ActionBar에서 Toolbar로 마이그레이션했습니다. 내 앱은 API 9를 지원해야합니다.
이 코드를 사용하여 탭을 다시 추가하는 방법이 있습니까? :
Toolbar toolbar = (Toolbar) findViewById(R.id.new_actionbar);
setSupportActionBar(toolbar);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
가능한 경우 사용자 지정 테마를 사용하거나 도구 모음의 스타일을 지정하려면 어떻게해야합니까?
문서에는 이것이 더 이상 사용되지 않으며 다른 유형의 탐색 사용을 제안한다고 나와 있습니다. 그러나 동일한 기능을 가진 Android의 다른 구성 요소를 알지 못합니다.
도움?
API 21에서는 메서드 setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)
가 더 이상 사용되지 않습니다 .
업데이트 01/08/2019 (Android Material Components)
2018 년 11 월 Android Material Components가 안정적으로 출시됨에 따라 Google은 머티리얼 구성 요소를 네임 스페이스
android.support.design
에서com.google.android.material
.
Material Component 라이브러리 는 Android의 디자인 지원 라이브러리를 대체합니다.
에 종속성을 추가하십시오 build.gradle
.
dependencies { implementation ‘com.google.android.material:material:1.0.0’ }
그런 다음 새로운 TabLayout을 사용할 수 있습니다 .
<androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.appbar.AppBarLayout ...>
<androidx.appcompat.widget.Toolbar .../>
<com.google.android.material.tabs.TabLayout
...
/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.constraintlayout.widget.ConstraintLayout>
코드는 간단합니다.
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(pager);
UPDATE 29/05/2015 (지원 라이브러리)
이제 새로운 디자인 지원 라이브러리를 통해 새로운 TabLayout을 사용할 수 있습니다 .
이 종속성을 build.gradle
compile 'com.android.support:design:22.2.0'
코드는 매우 간단합니다.
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
머티리얼 디자인의 많은 기능을 구현하려면 CoordinatorLayout 및 AppBarLayout 내에서 사용해야합니다 .
이 같은:
<android.support.design.widget.CoordinatorLayout
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.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar
...
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.design.widget.TabLayout
...
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
낡은
다른 패턴을 사용할 수 있습니다. 예를 들어 googleio14에서 볼 수있는 것과 동일한 예를 사용할 수 있습니다.
그것은 사용 SlidingTabLayout
와 함께 작동한다 ViewPager
.
여기에서 예제를 찾을 수 있습니다 (SDK 예제에 있음).
여기에서 Google io14 예제를 찾을 수 있습니다.
지금은이 질문에 답하기에는 조금 늦었지만 디자인 지원 라이브러리 사용과 Google I / O 이전에 모두 다루는 유사한 질문에 대한 답변을 작성했음을 깨달았습니다 .
아래에 필수 부분을 포함했습니다.
Android 디자인 지원 라이브러리 가 발표 된 이후 로 TabLayout
with를 사용하는 Toolbar
것이 훨씬 더 간단 해졌습니다. 즉, 더 이상 사용자 정의 뷰 클래스를 다운로드 할 필요가 없습니다.
Android 디자인 지원 라이브러리 의 Android 개발자 Blogspot 게시물에서 :
탭 :
탭을 통해 앱에서 서로 다른보기 사이를 전환 하는 것은 머티리얼 디자인에 대한 새로운 개념이 아니며 가정에서 최상위 탐색 패턴 으로 동일하게 또는 앱 내에서 다양한 콘텐츠 그룹 (예 : 다양한 장르의 음악)을 구성하는 데 사용됩니다.
디자인 라이브러리의 TabLayout 은보기의 너비가 모든 탭간에 균등하게 분할되는 고정 탭과 탭이 균일 한 크기가 아니고 가로로 스크롤 할 수있는 스크롤 가능한 탭을 모두 구현합니다. 프로그래밍 방식으로 탭을 추가 할 수 있습니다.
TabLayout tabLayout = ...;
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
그러나 탭 사이의 수평 페이징에 ViewPager 를 사용하는 경우 PagerAdapter 의 getPageTitle () 에서 직접 탭을 만든 다음을 사용하여 둘을 연결할 수 있습니다
setupWithViewPager()
. 이렇게하면 탭 선택 이벤트가 ViewPager를 업데이트하고 페이지 변경이 선택한 탭을 업데이트합니다.
Design Support 라이브러리를 사용하지 않는 경우 대신 다음을 수행해야합니다.
1. GitHub의 Google I / O Conference 앱에서 SlidingTabLayout.java
및 SlidingTabStrip.java
파일을 다운로드합니다 . 탭 레이아웃에서 사용되는보기이므로 '보기'라는 다른 Java 활동이있는 폴더를 만들어 거기에 배치했습니다.
2. 다음을 포함하도록 레이아웃을 편집합니다 SlidingTabLayout
.
<LinearLayout
android:orientation="vertical"
... >
<!-- This is the Toolbar with the tabs underneath -->
<LinearLayout
android:orientation="vertical"
... >
<include android:id="@+id/my_toolbar" layout="@layout/toolbar" />
<com.mycompany.myapp.SlidingTabLayout
android:id="@+id/sliding_tabs"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- This is the ViewPager (which you already should have if you have
used tabs before) -->
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
...
</LinearLayout>
The line which references the Toolbar
(<include android:id="@+id/detail_toolbar" layout="@layout/toolbar" />
), is referencing another XML file I had used to make the Toolbar
.
3. Change the package names in SlidingTabLayout.java
and SlidingTabStrip.java
corresponding to where they were placed. In my case, I used something similar to: package com.mycompany.myapp.view;
for both of these files. Organise imports and add in any necessary, as noted by the IDE you are using.
4. In your Activity
(which was extending AppCompatActivity
), setup the Toolbar
in the onCreate
method:
Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
setSupportActionBar(toolbar);
5. Setup the ViewPager
and SlidingTabLayout
parts:
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.tab_line));
mSlidingTabLayout.setDistributeEvenly(true);
mSlidingTabLayout.setViewPager(mViewPager);
The colour 'tab_line
' was a colour I had declared in color.xml
which would be the colour of the tab line indicator. Also note that the variables above were global which I defined previously in this activity:
SlidingTabLayout mSlidingTabLayout;
ViewPager mViewPager;
6. Finally, setup the ViewPagerAdapter
which I had called earlier. This would be responsible for changing the page depending on which tab was selected. I used the following code:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
// Returns the number of tabs
return 3;
}
@Override
public Fragment getItem(int position) {
// Returns a new instance of the fragment
switch (position) {
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
As I mentioned above, further details to these solutions are available on another question I answered, about using Sliding Tabs with the Toolbar.
참고URL : https://stackoverflow.com/questions/26540078/use-tab-with-new-toolbar-appcompat-v7-21
'Program Tip' 카테고리의 다른 글
Swift의 지수 연산자 (0) | 2020.11.01 |
---|---|
Swift에서 한 줄씩 파일 / URL 읽기 (0) | 2020.10.31 |
Anaconda Python은 Windows에서 어디에 설치됩니까? (0) | 2020.10.31 |
물음표와 함께 작은 검은 색 다이아몬드를 보여주는 PHP 출력 (0) | 2020.10.31 |
퍼블릭 데이터 멤버 vs 게터, 세터 (0) | 2020.10.31 |