Как поместить круг над прямоугольником в XML

#android #xml #android-layout #layout

Вопрос:

Я хочу сделать этот макет (круг над прямоугольником)

введите описание изображения здесь

вот где я застрял

Комментарии:

1. Вы должны попробовать самостоятельно и опубликовать, если у вас возникнут какие-либо проблемы

2. Я пытался, но не достиг этой компоновки

3. можете ли вы поделиться своей работой?.. что вы сделали до сих пор?

4. какой тип макета вы используете? Этого можно легко достичь с помощью ConstraintLayout

5. компоновка ограничений

Ответ №1:

Я решил эту проблему

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".activity.fragments.profile_page">

<View
    android:id="@ id/view_header"
    android:layout_width="match_parent"
    android:layout_height="160dp"
    android:background="@color/purple_500"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@ id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:letterSpacing="1"
    android:paddingVertical="10dp"
    android:text="@string/app_name"
    android:textAlignment="center"
    android:textColor="@color/white"
    android:textSize="30sp"
    android:textStyle="bold" />

<ImageView
    android:id="@ id/imageView"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="@drawable/profile_circular_image_background"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@ id/textView" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="@color/purple_200"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@ id/imageView" />


</androidx.constraintlayout.widget.ConstraintLayout>
 

Ответ №2:

Я использовал макет ограничений для создания этого макета.

смотрите это изображение макета

Во-первых, создайте файл ресурсов для рисования в своем приложении для рисования.

перейдите в раздел «Рисование» — > Щелкните правой кнопкой мыши ->> Создать ->>> Файл ресурсов для рисования ->>>> Укажите Любое имя

 <?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval"
       xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:color="@color/white" android:width="2dp"/>
    <solid android:color="@color/black" />
</shape>
 

Во-вторых, создайте свой макет в своем действии/фрагменте

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    tools:context=".FragmentA">making
    <ImageView
        android:id="@ id/imageView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@color/teal_200"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@ id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Jonathan Wick"
        android:textColor="@color/white"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="@id/imageView"
        app:layout_constraintEnd_toEndOf="@id/imageView"
        app:layout_constraintStart_toStartOf="@id/imageView"
        app:layout_constraintTop_toTopOf="@id/imageView" />

    <ImageView
        android:id="@ id/circle"
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/circle"
        app:layout_constraintBottom_toTopOf="@id/tvDashBoard"
        app:layout_constraintEnd_toEndOf="@id/imageView"
        app:layout_constraintStart_toStartOf="@id/imageView"
        app:layout_constraintTop_toBottomOf="@id/tvName" />

    <TextView
        android:id="@ id/tvDashBoard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_marginTop="30dp"
        android:drawableStart="@drawable/ic_dashboard"
        android:drawablePadding="10dp"
        android:text="Dashboard"
        android:textColor="@color/white"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/circle" />

</androidx.constraintlayout.widget.ConstraintLayout>