как поставить изображение на границе линейного макета в андроид студии - PullRequest
0 голосов
/ 04 октября 2018

Я хочу поместить изображение закладки в нижнюю границу линейного макета.По сути, это тип кнопки закладки, а мой линейный макет содержит вопрос, а изображение - кнопку закладки.Когда пользователь щелкнет по нему, вопрос будет сохранен.Но как я могу добавить изображение на границе линейного макета?Это макет, который я хочу разработать.

enter image description here

Может кто-нибудь сказать мне, как это сделать?Каким будет код xml для него.

Ответы [ 2 ]

0 голосов
/ 04 октября 2018

Согласно моему пониманию , если вы хотите создать перекрывающийся вид (например, закладку, как показано в вашем вопросе), вы должны использовать 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

Надеюсь, это поможет вам.

0 голосов
/ 04 октября 2018

Проверьте это

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#000">
        </LinearLayout>
        <ImageView
            android:src="@android:drawable/btn_plus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_gravity="end"
            />
    </FrameLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...