Согласно моему пониманию , если вы хотите создать перекрывающийся вид (например, закладку, как показано в вашем вопросе), вы должны использовать ConstraintLayout
вместо использования LinearLayout
или RelativeLayout
или любой другой макет, поскольку с помощью ConstraintLayout
вы можете легко создавать перекрывающиеся виды и использовать их.
Приведенный ниже код должен решить вашу проблему, если у вас есть некоторое понимание ConstraintLayout
.
1] activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- res/layout/activity_main.xml -->
<android.support.constraint.ConstraintLayout
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">
<TextView
android:id="@+id/textView"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:text="1. Who was the first prime minister of India?"
android:textColor="@android:color/black"
android:textStyle="bold|italic"
android:textSize="24sp"
android:paddingTop="10dp"
android:paddingStart="25dp"
android:paddingEnd="25dp"
android:paddingBottom="35dp"
android:background="@drawable/drawable_for_bookmark_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/btn_star"
android:contentDescription="Bookmark Icon"
android:tint="@android:color/black"
app:layout_constraintStart_toStartOf="@id/textView"
app:layout_constraintTop_toBottomOf="@id/textView"
app:layout_constraintEnd_toEndOf="@id/textView"
app:layout_constraintBottom_toBottomOf="@id/textView"
app:layout_constraintHorizontal_bias="0.95" />
</android.support.constraint.ConstraintLayout>
2] drawable_for_bookmark_view.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/drawable_for_bookmark_view.xml -->
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:color="#000"
android:width="3dp" />
<corners
android:radius="7dp" />
</shape>
3] Также добавьте эту зависимость ConstraintLayout
в build.gradle (Модуль: приложение) файл;в блоке зависимостей, как:
dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
}
Снимок экрана (кода выше):
![enter image description here](https://i.stack.imgur.com/TrJia.jpg)
Надеюсь, это поможет вам.