如何从android.support.v7.widget.Toolbar删除顶部和底部填充? [英] How to remove top and bottom padding from android.support.v7.widget.Toolbar?

查看:77
本文介绍了如何从android.support.v7.widget.Toolbar删除顶部和底部填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试放置

I am trying to place a SlidingTabLayout inside my android.support.v7.widget.Toolbar but for some reason there is extra top and bottom padding in portrait layout. As shown in this screenshot:

在横向布局中,android.support.v7.widget.Toolbar较短,并且多余的填充消失了:

In landscape layout the android.support.v7.widget.Toolbar is shorter and the extra padding is gone:

我知道contentInsertStartcontentInsetEnd属性,但顶部和底部似乎没有任何东西.这是我的布局:

I am aware of the contentInsertStart and contentInsetEnd attributes but there does not appear to be anything for top and bottom. Here is my layout:

<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="?attr/actionBarTheme"
    >

    <!-- Changing the size of the toolbar fixed the problem below but I don't like the solution since the height difference is perceptible -->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/colorPrimary"
        android:padding="0dp"
        app:popupTheme="?attr/actionBarPopupTheme"
        >

        <!-- TODO: BUG - This isn't filling out the action bar in portrait (see note above) -->
        <com.myapplication.views.widgets.SlidingTabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/pink_400"
            />

    </android.support.v7.widget.Toolbar>

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

如注释中所述,如果我将android.support.v7.widget.Toolbar的高度手动设置为48dp,则SlidingTabLayout会填满它,但是这里有两个问题:

As indicated in the comments if I manually set the height of the android.support.v7.widget.Toolbar to 48dp then the SlidingTabLayout fills it out but there are two problems here:

  1. 工具栏的高度与标准工具栏的高度不同,在更改活动时会很明显.
  2. 如果更改高度,Toolbar中的图标将不再垂直居中
  1. The toolbar is a different height than the standard toolbar which is noticeable when changing activities.
  2. The icons in the Toolbar are no longer vertically centered if I change it's height

因此,正如标题所述,如何从android.support.v7.widget.Toolbar中删除顶部和底部填充?

So as the title says, how do I remove top and bottom padding from android.support.v7.widget.Toolbar?

推荐答案

好吧,@ RaviSravanKumar的评论帮助我弄清楚了这一点.当我将布局更改回:

Ok so @RaviSravanKumar comment helped me figure this out. When I changed my layout back to:

<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="?attr/actionBarTheme"
    >

    <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="?attr/actionBarPopupTheme"
        >

        <com.myapplication.views.widgets.SlidingTabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="wrap_content"
            android:layout_height="?attr/actionBarSize"
            />

    </android.support.v7.widget.Toolbar>

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

将高度设置为?attr/actionBarSize时,我注意到SlidingTabLayout实际上填充了整个高度.我只是注意到了这一点,因为我设置了用于调试的粉红色背景.

With the heights set to ?attr/actionBarSize I noticed the SlidingTabLayout was actually filling the entire height. I only noticed this because of the pink background I had set for debugging.

我最初错过此事件的原因是因为下划线指示器仍不在底部(如原始问题的屏幕截图所示).我必须对SlidingTabLayout代码进行以下更改:

The reason I missed this originally was because the underline indicator was still not at the bottom (as shown in the screenshot in the original question). I had to make the following changes to the SlidingTabLayout code:

原始:

public SlidingTabLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    // Disable the Scroll Bar
    setHorizontalScrollBarEnabled(false);
    // Make sure that the Tab Strips fills this View
    setFillViewport(true);

    mTitleOffset = (int) (TITLE_OFFSET_DIPS * getResources().getDisplayMetrics().density);

    mTabStrip = new SlidingTabStrip(context);
    addView(mTabStrip, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}

新功能(请注意,从LayoutParams.WRAP_CONTENT更改为LayoutParams.MATCH_PARENT:

New: (note the change from LayoutParams.WRAP_CONTENT to LayoutParams.MATCH_PARENT:

public SlidingTabLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    // Disable the Scroll Bar
    setHorizontalScrollBarEnabled(false);
    // Make sure that the Tab Strips fills this View
    setFillViewport(true);

    mTitleOffset = (int) (TITLE_OFFSET_DIPS * getResources().getDisplayMetrics().density);

    mTabStrip = new SlidingTabStrip(context);
    addView(mTabStrip, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}

原始:

protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setLayoutParams(new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    TypedValue outValue = new TypedValue();
    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
            outValue, true);
    textView.setBackgroundResource(outValue.resourceId);

    if (Build.VERSION.SDK_INT >= 14) {
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    return textView;
}

新功能(请注意布局参数和填充的更改)

New: (note the change to the layout params and padding)

protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setLayoutParams(new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));

    TypedValue outValue = new TypedValue();
    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
            outValue, true);
    textView.setBackgroundResource(outValue.resourceId);

    if (Build.VERSION.SDK_INT >= 14) {
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, 0, padding, 0);

    return textView;
}

这篇关于如何从android.support.v7.widget.Toolbar删除顶部和底部填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆