如何从 android.support.v7.widget.Toolbar 中删除顶部和底部填充?
How to remove top and bottom padding from android.support.v7.widget.Toolbar?
我想在我的 android.support.v7.widget.Toolbar
中放置一个 SlidingTabLayout
,但出于某种原因,在纵向布局中有额外的顶部和底部填充。如此截图所示:
在横向布局中,android.support.v7.widget.Toolbar
更短,多余的填充消失了:
我知道 contentInsertStart
和 contentInsetEnd
属性,但顶部和底部似乎没有任何内容。这是我的布局:
<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
将其填满,但这里有两个问题:
- 工具栏的高度与标准工具栏的高度不同,这在更改活动时很明显。
- 如果我更改其高度,
Toolbar
中的图标将不再垂直居中
正如标题所说,如何从 android.support.v7.widget.Toolbar 中删除顶部和底部填充?
好的,@RaviSravanKumar 的评论帮助我解决了这个问题。当我将布局改回:
<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
实际上填满了整个高度。我之所以注意到这一点,是因为我为调试设置了粉红色背景。
原来我错过这个的原因是下划线指示器仍然不在底部(如原问题的屏幕截图所示)。我必须对 SlidingTabLayout
代码进行以下更改:
原文:
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
的变化:
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;
}
新:(注意布局参数和填充的变化)
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
中放置一个 SlidingTabLayout
,但出于某种原因,在纵向布局中有额外的顶部和底部填充。如此截图所示:
在横向布局中,android.support.v7.widget.Toolbar
更短,多余的填充消失了:
我知道 contentInsertStart
和 contentInsetEnd
属性,但顶部和底部似乎没有任何内容。这是我的布局:
<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
将其填满,但这里有两个问题:
- 工具栏的高度与标准工具栏的高度不同,这在更改活动时很明显。
- 如果我更改其高度,
Toolbar
中的图标将不再垂直居中
正如标题所说,如何从 android.support.v7.widget.Toolbar 中删除顶部和底部填充?
好的,@RaviSravanKumar 的评论帮助我解决了这个问题。当我将布局改回:
<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
实际上填满了整个高度。我之所以注意到这一点,是因为我为调试设置了粉红色背景。
原来我错过这个的原因是下划线指示器仍然不在底部(如原问题的屏幕截图所示)。我必须对 SlidingTabLayout
代码进行以下更改:
原文:
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
的变化:
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;
}
新:(注意布局参数和填充的变化)
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;
}