이것은 주요 활동의 내 코드입니다.
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를 확인해 보셨습니까? ?
OnTabSelectedListener
이벤트에 대한 리스너를 생성해야합니다 . 사용자가 탭을 선택할 때 tabLayout.setBackgroundColor(int color)
올바른 탭인지 확인한 다음을 사용하여 배경색을 변경 하거나 올바른 탭이 아닌 경우 다시 변경해야합니다. 동일한 방법으로 다시 일반 색상.
답변
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"/>
답변
나에게 가장 적합한 옵션을 찾았으므로 애니메이션에서도 작동합니다.
당신이 사용할 수있는 indicator
스스로를 배경으로 .
당신은 설정할 수 있습니다 app:tabIndicatorGravity="stretch"
배경으로 사용할 속성을 .
예:
<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>
도움이되기를 바랍니다.
답변
다음과 같이 각 탭의 배경 또는 잔물결 색상을 변경할 수 있습니다.
//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));
}
}
}
![](http://daplus.net/wp-content/uploads/2023/04/coupang_part-e1630022808943-2.png)