我在自定义视图中添加了相对布局,并在选项卡布局中添加了它。我对选项卡使用白色背景,并且没有在选项卡自定义布局中应用任何填充。但随后我也在选项卡上进行了填充,因此我在选项卡中显示了一些灰色背景,就好像 android 在内部将填充应用到选项卡一样。我正在尝试显示三个我能够执行的文本视图,但其中一个由于应用于选项卡的填充而被截断。我还希望选项卡具有一致的背景颜色。
这是我的代码:
activity_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.customtablayoutapplication.HomeActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_home" />
</android.support.design.widget.CoordinatorLayout>
fragment_home.xml:
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="@style/AppTheme.AppBarOverlay"
tools:context="com.customtablayoutapplication.HomeFragment"
tools:showIn="@layout/activity_home">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
custom_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="@android:color/white"
android:layout_height="match_parent"
android:layout_gravity="center">
<TextView
android:id="@+id/total_request_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="12"
android:textAllCaps="false"
android:visibility="gone"
android:textColor="@color/colorPrimaryDark"
android:textSize="12sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:fitsSystemWindows="true"
android:gravity="center">
<TextView
android:id="@+id/request_status"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="New Request"
android:textAllCaps="false"
android:textColor="@color/colorPrimaryDark"
android:textSize="12sp" />
<TextView
android:id="@+id/badge_icon"
android:layout_width="13dp"
android:layout_height="13dp"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/request_status"
android:layout_marginLeft="-3dp"
android:layout_marginTop="15dp"
android:background="@drawable/circular_text_background"
android:clickable="false"
android:visibility="gone" />
</RelativeLayout>
</RelativeLayout>
HomeFragment.java:
package com.customtablayoutapplication;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* A placeholder fragment containing a simple view.
*/
public class HomeFragment extends Fragment {
private ViewPager viewPager;
private TabLayout tabLayout;
public HomeFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setUpTabIcons();
return view;
}
private void setUpTabIcons() {
RelativeLayout tabNewRequest= (RelativeLayout) LayoutInflater.from(getActivity()).inflate(R.layout.custom_tab, null);
TextView tabNewRequesttxt = (TextView) tabNewRequest.findViewById(R.id.request_status);
tabNewRequesttxt.setText("New Request");
tabLayout.getTabAt(0).setCustomView(tabNewRequest);
RelativeLayout tabInProgress= (RelativeLayout) LayoutInflater.from(getActivity()).inflate(R.layout.custom_tab, null);
TextView tabInProgresstxt = (TextView) tabInProgress.findViewById(R.id.request_status);
tabInProgresstxt.setText("In Progress");
tabLayout.getTabAt(1).setCustomView(tabInProgress);
RelativeLayout tabWorkDone= (RelativeLayout) LayoutInflater.from(getActivity()).inflate(R.layout.custom_tab, null);
TextView tabWorkDonetxt = (TextView) tabWorkDone.findViewById(R.id.request_status);
tabWorkDonetxt.setText("Work Done");
tabLayout.getTabAt(2).setCustomView(tabWorkDone);
RelativeLayout tabDelivered= (RelativeLayout) LayoutInflater.from(getActivity()).inflate(R.layout.custom_tab, null);
TextView tabDeliveredtxt = (TextView) tabDelivered.findViewById(R.id.request_status);
tabDeliveredtxt.setText("Delivered");
tabLayout.getTabAt(3).setCustomView(tabDelivered);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new NewRequestFragment(), "New Request");
adapter.addFragment(new InProgressFragment(), "In Progress");
adapter.addFragment(new WorkDoneFragment(), "Work Done");
adapter.addFragment(new DeliveredFragment(), "Delivered");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
固定选项卡的白色背景周围有灰色背景:
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="210dp"
android:layout_height="28dp"
android:layout_centerInParent="true"
android:background="@drawable/bg_forum_tab"
app:tabIndicatorColor="@color/colorBtnBg"
app:tabIndicatorHeight="0dp"
app:tabPaddingBottom="-1dp"
app:tabPaddingEnd="-1dp"
app:tabPaddingStart="-1dp"
app:tabPaddingTop="-1dp"
app:tabSelectedTextColor="@color/colorBtnBg"
app:tabTextColor="@color/colorWhite" />
像这样设置
tabPaddingStart
/tabPaddingEnd
/tabPaddingTop
/tabPaddingBottom
。
它对我有用:
在您的
TabLayout
中,您必须将 tabPaddingEnd
和 tabPaddingStart
设置为 0dp
。
在自定义视图中,您必须将
layout_width
和 layout_height
设置为 match_parent
,以便用您的自定义颜色填充所有空间。
选项卡布局:
<android.support.design.widget.TabLayout
android:id="@+id/tl_dashboard"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentBottom="true"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorRed"
app:tabMode="fixed"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"/>
和自定义视图:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Your widgets-->
</RelativeLayout>
通过 XML,您可以仅删除左右填充,如下所示:
app:tabPaddingEnd="0dp"
app:tabPaddingStart="0dp"
注意,这种方式不适用于顶部和底部填充,但我找到了下一个解决方案:
当您使用自定义视图作为选项卡项时,您需要为视图设置 LayoutParams 并设置 paddings 0。
for (int i = 0; i < tabLayout.getTabCount(); i++) {
View tabView = LayoutInflater.from(context)
.inflate(LayoutInflater.from(this), R.layout.item_main_tabview, null, false);
tabView.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
tabView.setPadding(0, 0, 0, 0);
tabLayout.getTabAt(i).setCustomView(tabViewBinding.getRoot());
}
我知道这是一个老问题,但这可以帮助其他人。填充开始和填充结束不会删除选项卡之间的空间。解决方案是:
android:clipToPadding =“true”
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/colorGrayBackground"
app:tabGravity="fill"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
app:tabContentStart="0dp"
app:tabIndicatorHeight="0dp"
android:clipToPadding="true"
app:tabMode="fixed" />
然后您可以将填充设置为“0dp”。希望它对某人有帮助。
在向选项卡布局添加新选项卡时,我通过将自定义视图的父级的边距和填充设置为零来解决此问题。
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(0,0,0,0);
TabLayout.Tab newTab = mTabsBottom.newTab().setCustomView(R.layout.view_custom_tab);
setupTabParentLayout(newTab.getCustomView(), lp);
..
..
..
private void setupTabParentLayout(View customView, LinearLayout.LayoutParams lp) {
LinearLayout tabParent = (LinearLayout) customView.getParent();
tabParent.setLayoutParams(lp);
tabParent.setPadding(0,0,0,0);
}
这里的技巧是使用 LinearLayout.LayoutParams 作为自定义视图的父级。
首先我尝试使用
app:tabMinWidth="0dp"
然后我尝试了dcanbatman的答案。
它开始仅适用于这两种解决方案:
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
app:tabMinWidth="0dp"
如果您尝试了很多方法但都不起作用,这就是我最终所做的:只需设置
android:layout_height =“20dp”
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_gravity="bottom"
android:layout_height="130dp"/>
<com.google.android.material.tabs.TabLayout
android:layout_height="20dp"
android:layout_width="match_parent"
android:layout_gravity="bottom"
app:tabBackground="@drawable/tab_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp"/>
</FrameLayout>
我从代码创建 TabLayout 并且我只提供帮助
for (int position = 0; position < mTabLayout.getTabCount(); position++) {
TabLayout.Tab tab = mTabLayout.getTabAt(position);
if (tab != null) {
ViewCompat.setPaddingRelative(tab.view, 0, 0, 0, 0);
}
}
<android.support.design.widget.TabLayout
android:id="@+id/tabview"
app:layout_constraintTop_toBottomOf="@+id/ivpromo"
android:layout_width="0dp"
app:tabMode="scrollable"
app:tabIndicatorHeight="0dp"
app:tabContentStart="0dp"
android:clipToPadding="true"
app:tabMaxWidth="45dp"
app:tabGravity="fill"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:tabIndicatorColor="@android:color/transparent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@+id/tvcode"
android:layout_height="40dp"/>
当我对 TabLayout 的项目使用自定义布局时,我使用:
app:tabGravity="fill"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed"
app:tabPaddingEnd="0dp"
app:tabPaddingStart="0dp"
app:tabRippleColor="@null"
但是在这里,下面的代码应该可以完成工作:
app:tabPaddingEnd="0dp"
app:tabPaddingStart="0dp"
应用程序:tabPadding =“-10dp”
对我有用。 (-2、-5 不改变任何内容>