我正在嘗試使用自定義工具列開發一個應用程式,它與螢屏上的其他內容重疊。我有一個回收器視圖,我用它來顯示帶有文本的卡片視圖。
在設計視圖中,它似乎沒問題,并顯示回收器視圖位于工具列下方且暢通無阻。
Android Studio Design 視圖顯示的工具列不會阻礙回收器視圖。
但是,在啟動應用程式時,它明顯重疊。
應用程式啟動,內容隱藏在工具列后面。
活動_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
\android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<include
android:id="@ id/toolbar"
layout="@layout/toolbar"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
app:layout_constraintTop_toBottomOf="@id/toolbar"
android:background="@android:color/darker_gray"
android:padding="4dp"
android:scrollbars="vertical"
/>
</RelativeLayout>
工具列.xml
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple_200"
android:elevation="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:id="@ id/back_icon"
android:src="@drawable/ic_baseline_arrow_back_24" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_toEndOf="@id/back_icon"
android:layout_centerVertical="true"
android:text="@string/inventory_menu"
android:textSize="20sp"
android:textColor="@color/white"/>
<ImageView
android:id="@ id/add_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="@id/delete_icon"
android:layout_marginEnd="30dp"
android:src="@drawable/ic_baseline_add_circle_outline_24" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:id="@ id/delete_icon"
android:src="@drawable/ic_baseline_delete_24"/>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
示例_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="4dp"
android:layout_marginBottom="4dp">
<RelativeLayout
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp">
<TextView
android:id="@ id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line1"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:layout_alignParentTop="true" />
<TextView
android:id="@ id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 2"
android:textSize="15sp"
android:layout_below="@ id/textView"
android:layout_marginStart="8dp"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>enter code here
uj5u.com熱心網友回復:
我以前遇到過這個問題,我解決它的方式一直在調整android:layout_marginTop(recyclerview),直到沒有任何重疊。
uj5u.com熱心網友回復:
您可以嘗試使用 aCoordinatorLayout而不是RelativeLayout作為父級。看看這個教程。
隨著ConstraintLayout釋放你不應該使用 RelativeLayout,因為它的速度較慢,能力較差。
注意:CoordinatorLayout和ConstraintLayout不是一回事,并且具有非常不同的用例。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/326267.html
標籤:安卓 安卓布局 工具栏 android-工具栏
上一篇:錯誤-java.lang.ClassCastException:com.google.android.material.appbar.AppBarLayout無法轉換為androidx.appcomp
下一篇:xml視圖的重復資源ID
