【问题标题】:Make layout size depend on image size in XML使布局大小取决于 XML 中的图像大小
【发布时间】:2020-05-23 12:49:23
【问题描述】:

我目前正在使用 Android Studio 编写一个 Android 应用程序。为了适应大多数屏幕尺寸,我决定在 ConstraintLayout 中使用 LinearLayouts。 我有以下 xml 代码:

<?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/colorBackground"
tools:context=".Kadr1Activity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:contentDescription="@string/contentDescrKadr1"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@drawable/lol" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/backBtn"
            style="@style/Widget.AppCompat.ImageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_marginStart="16dp"
            android:adjustViewBounds="true"
            android:background="@android:color/transparent"
            android:contentDescription="@string/contentDescrNext"
            android:onClick="nextClick"
            android:scaleType="fitCenter"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="parent"

            app:srcCompat="@drawable/btn_back" />

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1"/>

        <ImageButton
            android:id="@+id/nextBtn"
            style="@style/Widget.AppCompat.ImageButton"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="end"
            android:layout_marginEnd="16dp"
            android:adjustViewBounds="true"
            android:background="@android:color/transparent"
            android:contentDescription="@string/contentDescrNext"
            android:onClick="nextClick"
            android:scaleType="fitCenter"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="parent"

            app:srcCompat="@drawable/btn_next" />


    </LinearLayout>

</LinearLayout>

看起来像这样:

我希望 ImageButtons 的高度取决于 ImageView 的高度。我尝试使用RelativeLayout,但是,我无法了解如何实现依赖关系,而不是整个屏幕大小,而只是ImageView 大小。

那么,如何将水平 LinearLayout(带按钮)的高度调整为比 ImageView 高度小 4 倍(例如)?

如果有任何可能的建议,将不胜感激。

【问题讨论】:

标签: android android-layout


【解决方案1】:

感谢@Marcin Orlowski,我明白了:

应该设置ImageView高度为“0dp”,水平LinearLayout高度为“0dp”,然后设置ImageView权重为“4”,LinearLayout权重为“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/colorBackground"
tools:context=".Kadr1Activity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:adjustViewBounds="true"
        android:contentDescription="@string/contentDescrKadr1"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@drawable/lol" />

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

        <ImageButton
            android:id="@+id/backBtn"
            style="@style/Widget.AppCompat.ImageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_marginStart="16dp"
            android:adjustViewBounds="true"
            android:background="@android:color/transparent"
            android:contentDescription="@string/contentDescrNext"
            android:onClick="nextClick"
            android:scaleType="fitCenter"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="parent"

            app:srcCompat="@drawable/btn_back" />

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1"/>

        <ImageButton
            android:id="@+id/nextBtn"
            style="@style/Widget.AppCompat.ImageButton"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="end"
            android:layout_marginEnd="16dp"
            android:adjustViewBounds="true"
            android:background="@android:color/transparent"
            android:contentDescription="@string/contentDescrNext"
            android:onClick="nextClick"
            android:scaleType="fitCenter"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="parent"

            app:srcCompat="@drawable/btn_next" />


    </LinearLayout>

</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多