Đây là mã của tôi trong hoạt động chính
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);
}
}
Và đây là mã của tôi trong 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>
Tôi muốn thay đổi màu nền của một tab khi được chọn
Bạn cũng có thể thêm một giá trị màu cho thuộc tính đó: ví dụ: app: tabBackground: @ color / colorAccent
– Trịnh Xuân Thái 21:45:46 27/06/2016Cảm ơn bạn cho giải pháp. Tuy nhiên, tôi đã mất hiệu ứng gợn mặc định với nó.
– Võ Minh Ân 23:58:45 13/07/2016Làm thế nào tôi có thể làm điều này theo chương trình?
– Tạ Mạnh Tuấn 14:54:15 01/03/2017@TSR, nếu bạn không cần các màu khác nhau cho mỗi tab, bạn có thể làm
– Tạ Phúc Nguyên 15:04:14 11/08/2017tabLayout.setBackgroundColor(colorInt)
. Nếu bạn cần nó cho mỗi tab, bạn có thể trích xuất trẻ em từ TabLayout của mìnhđể cải thiện giao diện, tôi nghĩ chúng ta cũng nên thêm state_pressed : <item android: drawable = "@ color / tab_background_selected" android: state_pressed = "true" />
– Hồ Phượng Loan 05:03:54 08/04/2019