TabLayout을 사용할 때 탭 배경색을 어떻게 변경합니까?
이것은 주요 활동의 내 코드입니다.
public class FilterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
PageAdapter pageAdapter = new PageAdapter(getSupportFragmentManager(), FilterActivity.this);
viewPager.setAdapter(pageAdapter);
// Give the TabLayout the ViewPager
final TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
}
그리고 이것은 XML의 내 코드입니다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar">
</include>
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="fill_parent"
style="@style/MyCustomTabLayout"
android:layout_height="48dp"/>
<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>
한 탭을 선택하면 배경색을 변경하고 싶습니다.
무엇 결국 나를 위해 일한 것은 @가如果我是DJ 제안 것과 유사하지만이 tabBackground
에 있어야 layout
파일을 하지 내부 style
가 보이는, 그래서 :
res/layout/somefile.xml
:
<android.support.design.widget.TabLayout
....
app:tabBackground="@drawable/tab_color_selector"
...
/>
및 선택기 res/drawable/tab_color_selector.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/tab_background_selected" android:state_selected="true"/>
<item android:drawable="@color/tab_background_unselected"/>
</selector>
이것을 시도 할 수 있습니다.
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabBackground">@drawable/background</item>
</style>
백그라운드 xml 파일에서 :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@color/white" />
<item android:drawable="@color/black" />
</selector>
xml에 속성을 추가합니다.
<android.support.design.widget.TabLayout
....
app:tabBackground="@drawable/tab_color_selector"
...
/>
그리고 드로어 블 폴더 tab_color_selector.xml에 생성합니다.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/tab_background_selected" android:state_selected="true"/>
<item android:drawable="@color/tab_background_unselected"/>
</selector>
API를 확인해 보셨습니까 ?
You will need to create a listener for the OnTabSelectedListener
event, then when a user selects any tab you should check if it is the correct one, then change the background color using tabLayout.setBackgroundColor(int color)
, or if it is not the correct tab make sure you change back to the normal color again with the same method.
You can have it in the xml.
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
app:tabTextColor="@color/colorGray"
app:tabSelectedTextColor="@color/colorWhite"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
As I found best and suitable option for me and it will work with animation too.
You can use indicator
it self as a background.
You can set app:tabIndicatorGravity="stretch"
attribute to use as background.
Example:
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorGravity="stretch"
app:tabSelectedTextColor="@color/white"
app:tabTextColor="@color/colorAccent">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chef" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User" />
</android.support.design.widget.TabLayout>
Hope it will helps you.
You can change the background color of the tab by this attribute
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
style="@style/CategoryTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
'android:background="@color/primary_color"/>'
You can change the background or ripple color of each Tab like this:
//set ripple color for each tab
for(int n = 0; n < mTabLayout.getTabCount(); n++){
View tab = ((ViewGroup)mTabLayout.getChildAt(0)).getChildAt(n);
if(tab != null && tab.getBackground() instanceof RippleDrawable){
RippleDrawable rippleDrawable = (RippleDrawable)tab.getBackground();
if (rippleDrawable != null) {
rippleDrawable.setColor(ColorStateList.valueOf(rippleColor));
}
}
}
One of simplest solution is to change colorPrimary from colors.xml file.
'program story' 카테고리의 다른 글
O (1), O (n log n) 및 O (log n) 복잡도를 갖는 알고리즘의 예 (0) | 2020.08.17 |
---|---|
파이썬에서 nosetest / unittest로 출력을 어설 션하는 방법은 무엇입니까? (0) | 2020.08.17 |
MySQL의 숨겨진 기능 (0) | 2020.08.17 |
부트 스트랩 모달이 열려 있는지 확인하는 방법으로 jquery validate를 사용할 수 있습니다. (0) | 2020.08.17 |
rubytutorial을 따르려는 Windows의 ExecJS :: RuntimeError (0) | 2020.08.17 |