我们如何缩小标签布局文本与其指标之间的差距

时间:2018-03-16 12:58:35

标签: android android-layout android-tablayout

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/htab_maincontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/htab_appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorBlack"
                android:fitsSystemWindows="false"
                android:theme="@style/AppTheme">

                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/htab_collapse_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="256dp"
                    android:fitsSystemWindows="true"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed"
                    app:titleEnabled="false">

                    <ImageView
                        android:id="@+id/htab_header"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/logo_inner"
                        android:fitsSystemWindows="true"
                        app:layout_collapseMode="parallax" />

                    <android.support.v7.widget.Toolbar
                        android:id="@+id/htab_toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:background="@android:color/transparent"
                        app:contentInsetStart="0dp"
                        app:layout_collapseMode="pin"
                        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

                    <android.support.design.widget.TabLayout
                        android:id="@+id/htab_tabs"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom"
                        android:background="@color/colorBlack"
                        app:tabGravity="center"
                        app:tabIndicatorColor="@color/colorSelectText"
                        app:tabPaddingEnd="@dimen/space_micro"
                        app:tabPaddingStart="@dimen/space_micro"
                        app:tabSelectedTextColor="@color/colorSelectText"
                        app:tabTextAppearance="@style/TabLayoutTextStyle"
                        app:tabTextColor="@color/colorWhite">


                    </android.support.design.widget.TabLayout>


                </android.support.design.widget.CollapsingToolbarLayout>

            </android.support.design.widget.AppBarLayout>



        </android.support.design.widget.CoordinatorLayout>

我想减少Tab Layout文本与其​​指标之间的空间。 我正在使用带有折叠工具栏的标签布局,当它向上滑动时,标签布局将固定在顶部。

我有4个标签及其相应的指示灯。

我正在分享我的xml&amp;也有附加图像。

enter image description here

5 个答案:

答案 0 :(得分:2)

您应该设置以下属性

app:tabPaddingBottom="-10dp"

答案 1 :(得分:1)

试试这个

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/htab_maincontent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/htab_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#2fff00"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/htab_collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:titleEnabled="false">


            <android.support.v7.widget.Toolbar
                android:id="@+id/htab_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent"
                app:contentInsetStart="0dp"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

            <ImageView
                android:id="@+id/htab_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher_background"
                app:layout_collapseMode="parallax" />


        </android.support.design.widget.CollapsingToolbarLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/htab_tabs"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_gravity="bottom"
            android:background="@drawable/tab_selector"
            app:layout_anchor="@+id/MyAppbar"
            app:layout_anchorGravity="bottom"
            app:tabGravity="fill"
            app:tabIndicatorColor="#F21757"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="#F21757"
            app:tabTextColor="@android:color/white" />


    </android.support.design.widget.AppBarLayout>

    <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" />

</android.support.design.widget.CoordinatorLayout>

<强> drawable/tab_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:state_pressed="false">
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">

            <item android:top="-2dp" android:left="-5dp" android:right="-5dp" android:bottom="2dp">
                <shape android:shape="rectangle">

                    <solid android:color="@android:color/white"/>
                    <stroke android:color="@color/colorPrimary" android:width="2dp"/>

                </shape>
            </item>
        </layer-list>
    </item>

    <item android:state_selected="true" android:state_pressed="true">
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">

            <item android:top="-2dp" android:left="-5dp" android:right="-5dp" android:bottom="2dp">
                <shape android:shape="rectangle">

                    <solid android:color="@android:color/white"/>
                    <stroke android:color="@color/colorPrimary" android:width="2dp"/>

                </shape>
            </item>
        </layer-list>
    </item>

</selector>

答案 2 :(得分:0)

尝试使用此自定义TabLayout并根据需要设计TextView。默认TextView可能有边距或填充。如果有效,请告诉我。

public class CustomTabLayout extends TabLayout{

        private boolean useCustomTab;

        public CustomTabLayout(Context context) {
            super(context);
        }

        public CustomTabLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        @Override
        public void addTab(@NonNull Tab tab, boolean setSelected) {
            if (useCustomTab){
                TextView tv = (TextView) View.inflate(getContext(), R.layout.tab_text, null);
                tv.setText(tab.getText());
                tab.setCustomView(tv);
            }
            super.addTab(tab, setSelected);
        }

        public void setUseCustomTab(boolean useCustomTab){
            this.useCustomTab = useCustomTab;
        }

    }

答案 3 :(得分:0)

简单地,我们可以根据我们的要求将 tabPaddingBottom 设置为任何负值,例如 -15dp

tabPaddingBottom = "-15dp" 为我工作。

enter image description here

其他参数请参考:https://material.io/components/tabs/android#scrollable-tabs

答案 4 :(得分:-1)

我知道这来晚了,但是对于以后可能遇到相同问题的任何人来说,下面的方法对我来说都是有效的。 在您的CardView中,添加:

app:cardElevation="0dp"
app:cardUseCompatPadding="false"