Android 如何将应用程序栏布局的内容与水平对齐
我有一个具有协调器布局的活动,我有应用程序栏布局,并包含滚动内容 默认情况下,应用程序栏布局的所有项目都是垂直排列的,而且当我覆盖应用程序栏布局的菜单项时,它不可见,因此我在应用程序栏布局中使用了一个按钮 我试图通过写入xml将方向更改为水平方向,但滚动的内容会出现在应用程序栏布局上 请帮助我更正此问题,以及如何包含此布局的菜单项 XMLAndroid 如何将应用程序栏布局的内容与水平对齐,android,layout,android-appbarlayout,Android,Layout,Android Appbarlayout,我有一个具有协调器布局的活动,我有应用程序栏布局,并包含滚动内容 默认情况下,应用程序栏布局的所有项目都是垂直排列的,而且当我覆盖应用程序栏布局的菜单项时,它不可见,因此我在应用程序栏布局中使用了一个按钮 我试图通过写入xml将方向更改为水平方向,但滚动的内容会出现在应用程序栏布局上 请帮助我更正此问题,以及如何包含此布局的菜单项 XML AppBarLayout扩展了线性布局。因此,如果需要创建更复杂的布局,则必须将视图封装在另一个视图组中,例如相对视图或甚至是约束视图等 比如: <
AppBarLayout
扩展了线性布局
。因此,如果需要创建更复杂的布局,则必须将视图封装在另一个视图组
中,例如相对视图
或甚至是约束视图
等
比如:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvItem"
android:layout_width="wrap_content"
android:layout_height="25pt"/>
<TextView
android:id="@+id/tvElevatorNo"
android:layout_width="wrap_content"
android:layout_height="25pt"
android:layout_toEndOf="@id/tvItem"/>
<ImageButton
android:id="@+id/ibComplete"
android:layout_width="62dp"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
您可以像这样使用约束布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="@color/colorPrimary">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#252A4E"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvItem"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_margin="8dp"
android:gravity="center"
android:text="Item :"
android:textColor="#F8F6F6"
android:textSize="15pt"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvElevatorNo"
android:layout_width="wrap_content"
android:layout_height="25pt"
android:layout_gravity="center_vertical"
android:layout_marginStart="8dp"
android:gravity="center"
android:text="1"
android:textColor="#F8F6F6"
android:textSize="15pt"
app:layout_constraintBottom_toBottomOf="@+id/tvItem"
app:layout_constraintStart_toEndOf="@+id/tvItem"
app:layout_constraintTop_toTopOf="@+id/tvItem" />
<ImageButton
android:id="@+id/ibComplete"
android:layout_width="62dp"
android:layout_height="62dp"
android:layout_marginEnd="8dp"
android:background="#252A4E"
android:onClick="AddElevator"
android:tint="#F9FBE7"
app:layout_constraintBottom_toBottomOf="@+id/tvElevatorNo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/tvElevatorNo"
tools:srcCompat="@drawable/ic_back" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_add_item"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/constraintLayout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
你能分享xmlYeah吗?当然,我会上传使用菜单项和菜单的解决方案。通常,你必须添加工具栏,菜单将被添加到工具栏中
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="@color/colorPrimary">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#252A4E"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvItem"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_margin="8dp"
android:gravity="center"
android:text="Item :"
android:textColor="#F8F6F6"
android:textSize="15pt"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvElevatorNo"
android:layout_width="wrap_content"
android:layout_height="25pt"
android:layout_gravity="center_vertical"
android:layout_marginStart="8dp"
android:gravity="center"
android:text="1"
android:textColor="#F8F6F6"
android:textSize="15pt"
app:layout_constraintBottom_toBottomOf="@+id/tvItem"
app:layout_constraintStart_toEndOf="@+id/tvItem"
app:layout_constraintTop_toTopOf="@+id/tvItem" />
<ImageButton
android:id="@+id/ibComplete"
android:layout_width="62dp"
android:layout_height="62dp"
android:layout_marginEnd="8dp"
android:background="#252A4E"
android:onClick="AddElevator"
android:tint="#F9FBE7"
app:layout_constraintBottom_toBottomOf="@+id/tvElevatorNo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/tvElevatorNo"
tools:srcCompat="@drawable/ic_back" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_add_item"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/constraintLayout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>