Вкладка WhatsApp:
Текущая реализация моей вкладки:
Я хотел бы уменьшить ширину вкладки первой вкладки до warp_content
и освободить оставшееся пространстворазделите поровну между 3 другими вкладками.(Как и в WhatsApp)
Я сослался на SO сообщения с похожими вопросами, и не смог найти никакого полезного решения.
Примечание: Я использую пользовательские вкладки.
Код:
TabLayout
<com.google.android.material.tabs.TabLayout
android:id="@+id/main_activity_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:elevation="0dp"
app:tabIndicatorColor="@color/tabIndicator"
app:tabIndicatorHeight="3dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/tabSelectedText"
app:tabTextColor="@color/tabText" />
Соединение с ViewPager:
// Setup view pager
private void setupViewPager() {
ViewPager viewPager = mainActivityBinding.mainActivityViewPager;
MyPagerAdapter myPagerAdapter =
new MyPagerAdapter(getSupportFragmentManager(), MainActivity.this);
mainActivityBinding.mainActivityViewPager.setAdapter(myPagerAdapter);
// Give the TabLayout the ViewPager
mainActivityBinding.mainActivityTabLayout.setupWithViewPager(viewPager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < mainActivityBinding.mainActivityTabLayout.getTabCount(); i++) {
TabLayout.Tab tab = mainActivityBinding.mainActivityTabLayout.getTabAt(i);
tab.setCustomView(myPagerAdapter.getTabView(i));
}
}
MyPagerAdapter.java
public class MyPagerAdapter extends FragmentPagerAdapter {
// Data
private String[] tabTitles = new String[]{"", "CHATS", "STATUS", "CALLS"};
private Context context;
// Constructor
public MyPagerAdapter(FragmentManager fragmentManager, Context context) {
super(fragmentManager);
this.context = context;
}
@Override
public int getCount() {
return tabTitles.length;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return ChatsFragment.newInstance();
case 1:
return ChatsFragment.newInstance();
case 2:
return StatusFragment.newInstance();
case 3:
return CallsFragment.newInstance();
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
public View getTabView(int position) {
CustomTabBinding customTabBinding = CustomTabBinding.inflate(LayoutInflater.from(context));
customTabBinding.customTabTitleTextView.setText(tabTitles[position]);
if (tabTitles[position].isEmpty()) {
customTabBinding.customTabImageView.setVisibility(View.VISIBLE);
}
return customTabBinding.getRoot();
}
}
custom_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/custom_tab_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@color/tabText"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_camera_alt_black_24dp"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/custom_tab_title_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:singleLine="true"
android:textColor="@color/tabText"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="First"
tools:textColor="@android:color/black" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>