簡體   English   中英

LinearLayout中的ImageView

[英]ImageView in LinearLayout

這種布局有兩個問題:

<?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中放置不同的Images時,發生了這種情況:

  • ImageViews的高度大於130dp,但是我將高度設置為(match_parent),而父高度為130dp。
  • ImageViews的寬度不同,但是ImageViews的權重相同,但是較大的ImageViews的寬度要大於另一個。

此代碼有效,為什么? 我真的不知道。

 <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>

您忘記在根布局中添加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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM