Как согласовать высоту и ширину 1: 1 в макете android - PullRequest
1 голос
/ 10 июля 2020

У меня есть изображение в Linearlayout. Я хочу, чтобы его ширина была fill_parent. Я хочу, чтобы его высота была такой, какой будет ширина. Например:

<LinearLayout 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="wrap_content"
    android:orientation="vertical">

    <ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="1:1"

    android:scaleType="centerCrop"
    android:layout_margin="2dp"
    tools:ignore="Suspicious0dp" />
</LinearLayout>

Но он не показывает изображения в макете android .. Если я принудительно изменил высоту просмотра изображения на 100dp в коде, это сработает, но мне интересно, почему высота изображения не соответствует ширине, даже если я написал constraintDimensionRatio 1: 1 ..

Спасибо

Ответы [ 2 ]

1 голос
/ 10 июля 2020

Вам нужно использовать ConstraintLayout в качестве родительского и установить измерение, которое вы хотите сделать основным, а другое - 0dp. app:layout_constraintDimensionRatio="H,1:1" означает изменить высоту и сделать ее 1: 1 с шириной.


 <androidx.constraintlayout.widget.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">

      
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="H,1:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
1 голос
/ 10 июля 2020

Попробуйте добавить weight к вашему imageview, тогда вы получите height, иначе вы должны использовать wrap_content как height.

<LinearLayout 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="wrap_content"
android:orientation="vertical">

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" 
android:scaleType="centerCrop"
android:layout_margin="2dp"
tools:ignore="Suspicious0dp" />
</LinearLayout>

 <!--or if you don't want to use weight then user  height = "wrap_content"-->
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...