Как создать плавающий макет в Android? - PullRequest
1 голос
/ 16 марта 2019

Мне нужно расположить макет, как показано на следующем рисунке, с желаемой шириной и высотой:

Отображение профиля Gmail с плавающей разметкой Gmail profile display floating layout

Ответы [ 2 ]

0 голосов
/ 27 марта 2019

1-й добавить это в файл Gradle

 implementation 'de.hdodenhof:circleimageview:2.1.0'

dialog.xml

<android.support.constraint.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="@drawable/bordershape">

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile_image"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:src="@drawable/userimage"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    android:layout_margin="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toEndOf="@id/profile_image"
    android:text="User Name"
    android:textColor="@android:color/black"
    android:textSize="12sp"
    android:layout_marginTop="30dp"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginBottom="5dp"
    android:gravity="center"
    android:id="@+id/name"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/name"
    app:layout_constraintStart_toEndOf="@id/profile_image"
    android:text="user@gmail.com"
    android:textColor="@android:color/black"
    android:textSize="15sp"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginBottom="5dp"
    android:gravity="center"
    android:id="@+id/email"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_margin="20dp"
    android:background="@drawable/round_border"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/profile_image"
    android:textAlignment="center"
    android:text="Manage your Google Account"
    android:gravity="center"
    android:id="@+id/googleaccount"
    android:textSize="18sp"
    android:padding="10dp"/>

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="@android:color/darker_gray"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/googleaccount"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/view1"/>

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/person"
    app:layout_constraintTop_toBottomOf="@+id/view1"
    app:layout_constraintStart_toStartOf="parent"
    android:layout_margin="20dp"
    app:civ_border_width="2dp"
    app:civ_border_color="@android:color/darker_gray"/>

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/person"
    app:layout_constraintTop_toBottomOf="@+id/profile1"
    app:layout_constraintStart_toStartOf="parent"
    android:layout_margin="20dp"
    app:civ_border_width="2dp"
    app:civ_border_color="@android:color/darker_gray" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/view1"
    app:layout_constraintStart_toEndOf="@id/profile1"
    android:text="Add Another Account"
    android:textAlignment="center"
    android:textStyle="bold"
    android:textColor="@android:color/black"
    android:textSize="18sp"
    android:gravity="center"
    android:id="@+id/account"
    android:layout_margin="20dp"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/account"
    app:layout_constraintStart_toEndOf="@id/profile2"
    android:text="Manage account on this Device"
    android:textColor="@android:color/black"
    android:textSize="18sp"
    android:textStyle="bold"
    android:textAlignment="center"
    android:gravity="center"
    android:id="@+id/device"
    android:layout_margin="20dp"/>

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="@android:color/darker_gray"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/device"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/view2"/>


<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="88dp"
    android:gravity="center"
    android:text="Privacy Policy"
    android:textAlignment="center"
    android:textColor="@android:color/darker_gray"
    android:textSize="13sp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/view2" />

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="60dp"
    android:gravity="center"
    android:text="Terms of Services"
    android:textAlignment="center"
    android:textColor="@android:color/darker_gray"
    android:textSize="13sp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    app:layout_constraintStart_toEndOf="@id/text1"
    app:layout_constraintTop_toBottomOf="@+id/view2" />

border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:innerRadius="50px">

<corners android:radius="10dp" />
<stroke android:width="1px" android:color="@android:color/black" />

round_border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<corners android:radius="35dp" />
<solid android:color="@android:color/transparent" />
<stroke
    android:width="1dp"
    android:color="@android:color/darker_gray" />

MainActivity

       btn.setOnClickListener(v -> {


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater layoutInflaterAndroid = LayoutInflater.from(this);
        View view2 = layoutInflaterAndroid.inflate(R.layout.dialog, null);
        builder.setView(view2);
        builder.setCancelable(false);
        final AlertDialog alertDialog = builder.create();
        alertDialog.show();

        view2.findViewById(R.id.account).setOnClickListener(v1 -> account());
        view2.findViewById(R.id.device).setOnClickListener(v12 -> device());


    });
0 голосов
/ 20 марта 2019

Использовать диалог для этого типа макетов, я пытался сделать этот макет.

Вот код XML для макета диалога:

floating_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/round_dialog_bg"
android:orientation="vertical"
android:layout_margin="@dimen/margin_10">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_margin="20dp"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/icon_user_image_male"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginLeft="5dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Abc"
                android:textSize="@dimen/text_12"
                android:gravity="center"
                android:textColor="@color/blackColor"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="abc@gmail.com"
                android:textSize="@dimen/text_12"
                android:gravity="center"
                android:textColor="@color/blackColor"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/margin_10"
                android:text="Manage Your Google Account"
                android:layout_marginTop="20dp"
                android:background="@drawable/manage_your_account_bg"/>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="0.5dp"
    android:background="@color/gray_hint_color"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/margin_10"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_user"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Another Account"
            android:textSize="@dimen/text_16"
            android:textStyle="bold"
            android:layout_marginLeft="20dp"
            android:textColor="@color/blackColor"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:layout_marginTop="@dimen/margin_30">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_user"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Manage account on this device"
            android:textSize="@dimen/text_16"
            android:textStyle="bold"
            android:layout_marginLeft="20dp"
            android:textColor="@color/blackColor"/>
    </LinearLayout>
</LinearLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="0.5dp"
    android:background="@color/gray_hint_color"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="@dimen/text_10"
    android:orientation="horizontal">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="@dimen/text_10">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Privacy Policy"
            android:textSize="@dimen/text_14"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="@dimen/text_10">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Terms of Services"
            android:textSize="@dimen/text_14"/>
    </LinearLayout>
</LinearLayout>
</LinearLayout>

Используйте плавающий_диалог.xml в своей активности при нажатии на любые виды, например кнопки.

Используйте приведенный ниже код для активности

private void openDialog() {
    final Dialog dialog = new Dialog(FloatingActivity.this);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.floating_dialog);
    dialog.show();
}

Выход:

Output for code

Надеюсь, это сработает для вас.

...