【问题标题】:ImageView in LinearLayout线性布局中的 ImageView
【发布时间】:2017-01-23 16:39:28
【问题描述】:

这个布局有两个问题:

<?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="130dp"
    android:orientation="horizontal"
    android:background="@null">
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/image1"
            android:id="@+id/image1"
            android:scaleType="centerCrop"
            android:background="@drawable/card_background"
            android:layout_weight=".5"
            />
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/image2"
            android:id="@+id/image2"
            android:scaleType="centerCrop"
            android:background="@drawable/card_background"
            android:layout_weight=".5"
            />
</LinearLayout>

当我在 ImageViews 中放置不同的图像时,发生了这种情况:

  • ImageViews 高度大于 130dp 但是我把高度设置为 (match_parent) 并且父高度是 130dp 。
  • ImageViews 的宽度不一样,但两个 ImageViews 的权重相同,但较大的一个比另一个占用更多的宽度。

【问题讨论】:

  • 你没有给父布局一个 weight_sum
  • 我做了,还是一样
  • 你的意思是图片的大小不一样,或者那些视图的大小不一样?您可以在 imageViews 中使用 scaletype=fitxy 使图像适合其边界
  • 显示输出图像以澄清
  • @Omer 为 true,但您可以使用 android:adjustViewBounds=true

标签: android android-layout imageview android-linearlayout


【解决方案1】:

你忘记在根布局中添加 weightsum,所以在根布局中添加这一行。

android:weightSum="1"

就像加了权重后的样子

  <?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="130dp"
    android:weightSum="1"
    android:orientation="horizontal"
    android:background="@null">
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/image1"
            android:id="@+id/image1"
            android:scaleType="centerCrop"
            android:background="@drawable/card_background"
            android:layout_weight=".5"
            />
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/image2"
            android:id="@+id/image2"
            android:scaleType="centerCrop"
            android:background="@drawable/card_background"
            android:layout_weight=".5"
            />
</LinearLayout>

【讨论】:

    【解决方案2】:

    这段代码有效,为什么? 我真的不知道。

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="130dp"
        android:background="@null"
        >
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight=".5"
                >
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="130dp"
                    android:id="@+id/image2"
                    android:scaleType="centerCrop"
                    android:src="@drawable/offer_mix_small_1"
                    />
            </RelativeLayout>
    
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight=".5"
                android:background="@color/secondary_text_color"
                >
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="130dp"
                    android:id="@+id/image1"
                    android:scaleType="centerCrop"
                    android:src="@drawable/offer_land"
                    />
            </RelativeLayout>
        </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      相关资源
      最近更新 更多