【发布时间】:2018-12-14 07:50:22
【问题描述】:
我已经创建了一个带有导航抽屉和菜单的工具栏,现在我试图在同一个工具栏上添加一个 textView 和 edittext 并将它们放在中心,但问题是当我添加相对布局并将宽度设置为匹配时parent,它会在左侧租用一些空间,并将要放置在中心的项目向右移动。
我已经粘贴了下面的代码:-
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="H E A D"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:id="@+id/ToolbarTextView"
style="@style/Base.TextAppearance.AppCompat.Large"
android:layout_marginTop="9dp"
android:textColor="@color/colorwhite"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ToolbarTextView"
android:layout_centerHorizontal="true"
android:text="Tagline"
android:textSize="15dp"
android:textColor="@color/colorwhite"/>
<EditText
android:layout_width="292dp"
android:layout_height="wrap_content"
android:background="@drawable/main_search"
android:padding="7dp"
android:backgroundTint="@color/colorwhite"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:hint="Search..."
android:id="@+id/ToolbarSearch1"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar></android.support.design.widget.AppBarLayout><include layout="@layout/content_main" /><android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
mainActivity的xml代码:-
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.strease.user.strease.MainActivity"
android:id="@+id/layout4"
tools:showIn="@layout/app_bar_main">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:paddingTop="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/></android.support.constraint.ConstraintLayout>
参考图如下:-
设备截图如下:-
P.S:- 我需要省略空格并将 editText 和 textViews 放到布局的中心(目前向右移动)
主Activity中的导航抽屉代码:-
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"><include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"/></android.support.v4.widget.DrawerLayout>
【问题讨论】:
标签: android android-layout android-actionbar