【问题标题】:ImageView inside LinearLayout - wrong ratioLinearLayout 内的 ImageView - 比例错误
【发布时间】:2016-09-16 12:08:48
【问题描述】:

我正在尝试为 RecyclerView 创建项目布局,其中两个主要元素的比例分别为 20% 和 80%。第一个元素是 ImageView,第二个是带有一些 TextViews 的 LinearLayout。基本上,它应该是这样的:

  20%          80%
+-----+-------------------+
| Img | Author, Date      |
+-----+ Title...          |
|                         |
+-------------------------+

所以,我创建了如下布局,但图像总是大于 20%。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

            <ImageView
                android:id="@+id/image"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:src="@drawable/image" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="4"
                android:layout_height="match_parent"
                android:orientation="vertical">

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

                    <TextView
                        android:id="@+id/author"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:id="@+id/date"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                </LinearLayout>

                <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

我正在尝试 layout_width 的所有组合,但比率总是有问题。

任何帮助将不胜感激。

【问题讨论】:

  • android:weightSum="5" 到主布局
  • @Pravin 不幸的是即使使用 weightSum ImageView 也太大了。

标签: android android-layout android-linearlayout android-imageview


【解决方案1】:

试试:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:orientation="horizontal">

            <ImageView
                android:id="@+id/image"
                android:layout_width="0dp"
                android:layout_weight=".2"
                android:layout_height="match_parent"
                android:src="@drawable/image" />

            <LinearLayout
                android:layout_width="0dp"
                 android:layout_weight=".8"
                android:layout_height="match_parent"
                android:orientation="vertical">

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

                    <TextView
                        android:id="@+id/author"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:id="@+id/date"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                </LinearLayout>

                <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>

【讨论】:

  • 很遗憾 ImageView 还是太大了。
【解决方案2】:

首先将 android:weightSum="5" 放在父线性布局中。其次不仅是比例,图像源的位图也很重要。在将其设置在 imageview 中之前,您可以使用缩短您的图像(图像资源)。 让我知道 weightsum 是否有效。

【讨论】:

  • 当我设置 weightSum 时没有任何改变。
  • 可能:因为没有图像源代码:可能是图像大小有问题,尝试使用 glide 并将您的图像 url 放在缩略图中。此外,您应该根据屏幕尺寸和布局比例缩放图像。
【解决方案3】:

您缺少将weightSum 提供给父布局。 在您的情况下,只需将 weightSum 添加到主布局LinearLayout。

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="5">

【讨论】:

  • 不幸的是即使使用 weightSum ImageView 也太大了。
【解决方案4】:

到目前为止,我已经了解到您想要创建一个列表行,那么您需要固定布局和图像视图的高度,如下所示。

<?xml version="1.0" encoding="utf-8"?>
 <android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/image"
            android:layout_width="0dp"
            android:layout_height="75dp"
            android:layout_weight="0.2"
            android:scaleType="centerInside"
            android:src="@drawable/image" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.8"
            android:orientation="vertical">

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

                <TextView
                    android:id="@+id/author"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多