<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
tools:parentTag="android.widget.RelativeLayout">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
app:titleMargin="0dp"
app:titleMarginEnd="0dp"
app:titleMarginStart="0dp"
app:titleMargins="0dp"/>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="@+id/custom_title"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:minHeight="?attr/actionBarSize">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/left_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="10dp"
android:visibility="gone"/>
<TextView
android:id="@+id/left_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@color/text_white_pressed_gray"
android:textSize="16sp"/>
</LinearLayout>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:drawablePadding="5dp"
android:gravity="center"
android:textColor="@color/color_white"
android:textSize="18sp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/right_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:drawablePadding="5dp"
android:gravity="center"
android:textColor="@color/text_white_pressed_gray"
android:textSize="16sp"/>
<ImageView
android:id="@+id/right_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
我想自定义布局,但custom_title布局始终位于工具栏下。
我不想在标签内放置custom_title。 如何使custom_title布局显示在工具栏的顶部? 为什么工具栏始终位于顶部?
由于工具栏是一个ViewGroup,它设置了更多的填充和子视图。如果将custom_title布局放入,则会导致custom_title无法居中。
公共类ToolbarLayout extends RelativeLayout实现了View.OnClickListener {
private static final int DEFAULT_TEXT_SIZE = 16;
private static final int DEFAULT_TITLE_SIZE = 18;
private static final int DEFAULT_TEXT_COLOR = Color.WHITE;
private AppCompatActivity activity;
private LayoutToolbarBinding binding;
private OnClickListener listener;
private String leftText, title, rightText;
private int leftColor, leftSize, leftImage, leftTextLIcon, leftTextRIcon;
private int titleSize, titleColor, titleTextIcon;
private int rightSize, rightImage, rightColor, rightTextLIcon, rightTextRIcon;
private boolean showBackup, showElevation;
private int backgroundColor;
public void setListener(OnClickListener listener) {
this.listener = listener;
}
public ToolbarLayout(Context context) {
this(context, null);
}
public ToolbarLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ToolbarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.layout_toolbar, this, true);
if (context instanceof AppCompatActivity) {
activity = (AppCompatActivity) context;
} else {
throw new IllegalArgumentException("This context is not a AppCompatActivity instance");
}
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ToolbarLayout);
showBackup = typedArray.getBoolean(R.styleable.ToolbarLayout_showBackup, true);
showElevation = typedArray.getBoolean(R.styleable.ToolbarLayout_showElevation, false);
leftText = typedArray.getString(R.styleable.ToolbarLayout_leftText);
title = typedArray.getString(R.styleable.ToolbarLayout_title);
rightText = typedArray.getString(R.styleable.ToolbarLayout_rightText);
leftColor = typedArray.getColor(R.styleable.ToolbarLayout_leftTextColor, DEFAULT_TEXT_COLOR);
titleColor = typedArray.getColor(R.styleable.ToolbarLayout_titleColor, DEFAULT_TEXT_COLOR);
rightColor = typedArray.getColor(R.styleable.ToolbarLayout_rightTextColor, DEFAULT_TEXT_COLOR);
leftSize = typedArray.getDimensionPixelSize(R.styleable.ToolbarLayout_leftTextSize, DEFAULT_TEXT_SIZE);
titleSize = typedArray.getDimensionPixelSize(R.styleable.ToolbarLayout_titleTextSize, DEFAULT_TEXT_SIZE);
rightSize = typedArray.getDimensionPixelSize(R.styleable.ToolbarLayout_rightTextSize, DEFAULT_TEXT_SIZE);
leftTextLIcon = typedArray.getResourceId(R.styleable.ToolbarLayout_leftTextLIcon, 0);
leftTextRIcon = typedArray.getResourceId(R.styleable.ToolbarLayout_leftTextRIcon, 0);
titleTextIcon = typedArray.getResourceId(R.styleable.ToolbarLayout_titleTextIcon, 0);
rightTextLIcon = typedArray.getResourceId(R.styleable.ToolbarLayout_rightTextLIcon, 0);
rightTextRIcon = typedArray.getResourceId(R.styleable.ToolbarLayout_rightTextRIcon, 0);
leftImage = typedArray.getResourceId(R.styleable.ToolbarLayout_leftImage, 0);
rightImage = typedArray.getResourceId(R.styleable.ToolbarLayout_rightImage, 0);
backgroundColor = typedArray.getColor(R.styleable.ToolbarLayout_backgroundColor, ResourceHelper.getColor(R.color.colorPrimaryDark));
typedArray.recycle();
setDefault();
}
private void setDefault() {
setLeftText(leftText);
setLeftColor(leftColor);
setLeftSize(leftSize);
setLeftDrawable(leftTextLIcon, leftTextRIcon);
setTitle(title);
setTitleColor(titleColor);
setTitleSize(titleSize);
setTitleTextIcon(titleTextIcon);
setRightText(rightText);
setRightColor(rightColor);
setRightSize(rightSize);
setRightDrawable(rightTextLIcon, rightTextRIcon);
setLeftImage(leftImage);
setRightImage(rightImage);
setClickListener(this);
//默认显示返回按键
showBackUpButton(showBackup);
setToolbarElevation(showElevation);
setBackgroundColor(backgroundColor);
}
答案 0 :(得分:0)
尝试使用elevation
属性,如下所示:
<RelativeLayout
...>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
...
android:elevation="0dp">
...
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="@+id/custom_title"
...
android:elevation="2dp">
...
</RelativeLayout>
</RelativeLayout>
答案 1 :(得分:0)
试试这个
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="RtlHardcoded">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
style="@style/matchwraplayout"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="300dp"
app:expandedTitleMarginBottom="94dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:expandedTitleTextAppearance="@style/CollapsingTextAppearance.Inverse">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="300dp"
app:expandedTitleMarginBottom="94dp"
android:layout_gravity="center"
android:gravity="center"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?colorPrimary">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:text="name"
android:layout_marginTop="50dp"
android:layout_marginBottom="20dp"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18sp"/>
</FrameLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:id="@+id/mTitle"
style="@style/toolbartitle"
android:layout_marginLeft="30dp"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
style="@style/matchwraplayout"
android:orientation="vertical">
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
答案 2 :(得分:0)
我可能错了,但我想,无论你想做什么都不适用于工具栏或ActionBar,其次你已经为你的应用创建了一个自定义工具栏,所以我猜你真的不需要在您的应用中包含工具栏。您只需在活动/活动的顶部添加自定义工具栏视图,并使用OnClick侦听器处理点击事件,这将是更好的方法。
layout_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/custom_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="6dp"
tools:parentTag="android.widget.RelativeLayout">
<RelativeLayout
android:id="@+id/custom_title"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:paddingEnd="16dp"
android:paddingStart="16dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/left_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="10dp"
android:src="@android:drawable/ic_input_add"
android:visibility="visible" />
<TextView
android:id="@+id/left_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Add"
android:textColor="@android:color/white"
android:textSize="16sp" />
</LinearLayout>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:drawablePadding="5dp"
android:gravity="center"
android:text="Title"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/right_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:drawablePadding="5dp"
android:gravity="center"
android:text="Search"
android:textColor="@android:color/white"
android:textSize="16sp" />
<ImageView
android:id="@+id/right_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:src="@android:drawable/ic_menu_search"
android:visibility="visible" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="apps.test.test.MainActivity">
<include layout="@layout/layout_toolbar" />
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World" />
</LinearLayout>