Как центрировать CardView внутри LinearLayout? - PullRequest
0 голосов
/ 05 апреля 2020

У меня есть портретное отображение, и я хочу перевести его в ландшафтный режим.

В ландшафтном режиме первый дочерний элемент LinearLayout, который также является LinearLayout, должен содержать CardView с ImageView (округлено, я не уверен, как это получится). Во втором LinearLayout я хотел бы, чтобы Имя пользователя и Пароль EditText располагались рядом друг с другом по горизонтали. И, наконец, последний LinearLayout должен содержать кнопки входа и регистрации также по горизонтали.

Я не знаю, как центрировать CardView внутри первого дочернего LinearLayout и как сделать ImageView округленным.

Вот что у меня есть: enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient"
    android:gravity="center"
    tools:context=".LoginActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1">


        <androidx.cardview.widget.CardView
            android:id="@+id/imageView"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            app:cardBackgroundColor="#090909"
            app:cardCornerRadius="250dp">


            <!--<androidx.cardview.widget.CardView
                android:layout_width="150dp"
                android:layout_height="145dp"
                android:gravity="center"
                app:cardCornerRadius="250dp">

            </androidx.cardview.widget.CardView>-->

        </androidx.cardview.widget.CardView>

        <!--<ImageView
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:scaleType="centerCrop"
            android:src="@drawable/login_logo" />-->

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <!--HERE WILL BE Username and Password-->

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
    <!--HERE WILL BE Login and Sign up-->

    </LinearLayout>


</LinearLayout>

Ответы [ 2 ]

2 голосов
/ 05 апреля 2020

android:gravity обрабатывает выравнивание своих дочерних элементов,

android:layout_gravity обрабатывает само выравнивание.

Если вы хотите более округленный вид карты, то исправьте высоту cardView.

Присвойте вес другой линейной раскладке "2", а раскладку с видом карты на вес "1" для небольшого размера.

Спасибо Вам! Happy Coding

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#abcdef"
    android:layout_gravity="center"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal">


        <androidx.cardview.widget.CardView
            android:id="@+id/imageView"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            app:cardBackgroundColor="#090909"
            app:cardCornerRadius="150dp">


            <!--<androidx.cardview.widget.CardView
                android:layout_width="150dp"
                android:layout_height="145dp"
                android:gravity="center"
                app:cardCornerRadius="250dp">

            </androidx.cardview.widget.CardView>-->

        </androidx.cardview.widget.CardView>

        <!--<ImageView
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:scaleType="centerCrop"
            android:src="@drawable/login_logo" />-->

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="horizontal">

        <!--HERE WILL BE Username and Password-->

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="horizontal">
        <!--HERE WILL BE Login and Sign up-->

    </LinearLayout>
</LinearLayout>

[Это ваш проект (изображение) => https://i.stack.imgur.com/68ARf.png]

1 голос
/ 05 апреля 2020

Ваша проблема проста. просто измените ориентацию вашей линейной раскладки следующим образом:

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" <!--change this-->
    android:layout_weight="1">


    <androidx.cardview.widget.CardView
        android:id="@+id/imageView"
        android:layout_width="150dp"
        android:layout_gravity="center"   <!-- and this-->
        android:layout_height="match_parent"
        app:cardBackgroundColor="#090909"
        app:cardCornerRadius="250dp">

    </androidx.cardview.widget.CardView>

    <!--<ImageView
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:scaleType="centerCrop"
        android:src="@drawable/login_logo" />-->

</LinearLayout>

после этой манипуляции ваше изображение карты будет в центре

...