我开始使用工具栏代替ActionBar。工具栏在每个布局中设置。但是,如您在图片中所见,带有工具栏的片段并非直接在状态栏下方开始。此外,工具栏会与网格布局中的文本一起崩溃。
这是activity_main.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:elevation="0.1dp"
app:itemIconTint="@drawable/nav_item_colors"
app:layout_constraintBottom_toBottomOf="@+id/nav_host_fragment"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
这是fragment_finder.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:backgroundTint="#ffffff"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_height="wrap_content"
tools:targetApi="21"
app:elevation="0dp"
android:layout_width="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
app:elevation="0dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Toolbar Title" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="7" >
<GridLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.5"
android:columnCount="3"
android:rowCount="1"
android:alignmentMode="alignMargins" >
<TextView
android:id="@+id/text_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="5dp"
android:fontFamily="@font/pacifico"
android:layout_gravity="left"
android:textSize="20sp" />
<TextView
android:id="@+id/text_finder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="5dp"
android:fontFamily="@font/pacifico"
android:textAlignment="center"
android:textSize="20sp" />
<TextView
android:id="@+id/text_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="5dp"
android:fontFamily="@font/pacifico"
android:layout_gravity="right"
android:textSize="20sp" />
</GridLayout>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_weight="5"
android:layout_height="0dp"
app:cardCornerRadius="15dp"
android:layout_margin="5dp"
android:elevation="10dp" >
<ImageView
android:id="@+id/profileview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ex1"
android:scaleType="centerCrop" />
</androidx.cardview.widget.CardView>
<GridLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:columnCount="3"
android:rowCount="1"
android:alignmentMode="alignMargins" >
<ImageView
android:id="@+id/schlecht"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/schlecht"
android:scaleType="centerCrop" />
<ImageView
android:id="@+id/hot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/hot"
android:scaleType="centerCrop" />
<ImageView
android:id="@+id/gut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/gut"
android:scaleType="centerCrop" />
</GridLayout>
</LinearLayout>
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我希望任何人都可以帮助我解决这个问题。
诚挚的问候 杰里米
答案 0 :(得分:4)
回答第一部分: 从activity_main
删除以下行android:paddingTop="?attr/actionBarSize"
在fragment_finder的“相对”布局中添加行
android:paddingTop="?attr/actionBarSize"
答案 1 :(得分:0)
使用工具栏时,您可以将其分配为操作栏,而无需在布局中添加填充,则可以使用以下代码将工具栏设置为操作栏
true