如何从android.support.v7.widget.Toolbar中删除顶部和底部填充?
问题描述:
我试图在我的android.support.v7.widget.Toolbar
中放置一个SlidingTabLayout
,但由于某些原因,在纵向布局中有额外的顶部和底部填充。如在该截图:如何从android.support.v7.widget.Toolbar中删除顶部和底部填充?
在景观布局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;
}
新:(注意改变布局PARAMS和填充)
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;
}
你尝试使用ATTR/actionBarSize的高度? –
@RaviSravanKumar,是的,我做到了。我会更新这个问题。 –
@RaviSravanKumar实际上你可能会做些什么。当我有'android:layout_height =“?attr/actionBarSize”'set时,我没有粉红色的背景。现在重置它显示'SlidingTabLayout'实际上确实填充了'工具栏',但指标很高(这就是为什么我最初没有注意到这种差异的原因)。 –