片段中的片段/工具栏显示在错误的位置
Fragment / Toolbar from fragment is shown at the wrong position
我开始使用工具栏而不是 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>
我希望有人能帮我解决这个问题。
谨致问候
杰里米
第一部分答案:
从 activity_main
中删除以下行
android:paddingTop="?attr/actionBarSize"
在 fragment_finder
中向相对布局添加行
android:paddingTop="?attr/actionBarSize"
当你使用 toolbar 时,你可以指定为 actionbar,这样,无需在布局中添加 padding,你可以使用下面的代码将 toolbar 设置为 actionbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
我开始使用工具栏而不是 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>
我希望有人能帮我解决这个问题。
谨致问候 杰里米
第一部分答案: 从 activity_main
中删除以下行android:paddingTop="?attr/actionBarSize"
在 fragment_finder
中向相对布局添加行android:paddingTop="?attr/actionBarSize"
当你使用 toolbar 时,你可以指定为 actionbar,这样,无需在布局中添加 padding,你可以使用下面的代码将 toolbar 设置为 actionbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);