You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:NestedScrollView布局下ImageView显示异常求助

解决NestedScrollView中ImageView的两个问题:绿边与Pending状态

Hey there! Let's work through those two ImageView issues you're hitting with your vertical product display setup. I've gone through your XML layout and found a couple of key issues causing these problems, plus straightforward fixes for each.

问题1:图片左侧出现绿边,无法铺满父布局

The biggest culprit here is a layout hierarchy mistake—you've placed your NestedScrollView inside the AppBarLayout, which breaks how CoordinatorLayout is supposed to work. AppBarLayout is designed to hold collapsible top components like Toolbars, while NestedScrollView needs to be a direct sibling of AppBarLayout within CoordinatorLayout to enable proper scrolling behavior. This misplacement is causing layout measurement glitches, leading to the green edge and the image not filling its parent.

Here's how to fix it:

  • Fix the layout hierarchy: Move NestedScrollView out of AppBarLayout so it's a direct child of CoordinatorLayout, alongside AppBarLayout.
  • Tweak the ImageView and Toolbar:
    • Add android:adjustViewBounds="true" to your ImageView—this works with scaleType="fitXY" to ensure the image properly scales to fill its parent.
    • Remove the left inset/padding from the Toolbar, which is likely causing that green edge. Add these attributes to your Toolbar:
      app:contentInsetLeft="0dp"
      app:contentInsetStart="0dp"
      android:padding="0dp"
      

问题2:图片显示pending状态

This "pending" state usually happens when the view isn't being initialized or rendered correctly, and again, the layout hierarchy error is the main cause here. When NestedScrollView is nested inside AppBarLayout, the entire view tree gets messed up, preventing the ImageView from finishing its draw cycle. Since you're using a local drawable (@drawable/omla3), it's not a network loading issue—just a layout problem.

Fix steps:

  • First, apply the layout hierarchy fix mentioned above.
  • Double-check that your @drawable/omla3 resource is valid (no corrupted files or incompatible formats).
  • If you later switch to loading images from the network (with libraries like Glide/Picasso), make sure your loading logic is correctly implemented (e.g., calling into(imageView) properly).

修正后的完整XML布局代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
    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" 
    tools:context=".ui.ItemDetialsActivity">

    <com.google.android.material.appbar.AppBarLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar 
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="parallax"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|enterAlways"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            android:padding="0dp">

            <ImageView 
                android:id="@+id/image_item_diteals"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@null"
                android:scaleType="fitXY"
                android:adjustViewBounds="true"
                android:src="@drawable/omla3"
                app:layout_collapseMode="parallax"/>
        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView 
        android:layout_width="match_parent"
        android:clipToPadding="false"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <androidx.cardview.widget.CardView 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardElevation="5dp"
                app:cardUseCompatPadding="true">

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

                    <TextView 
                        android:id="@+id/item_namedetails"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:padding="12dp"
                        android:text="Item name"
                        android:textColor="@color/colorPrimary"
                        android:textSize="20sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardElevation="8dp"
                app:cardUseCompatPadding="true">

                <TextView 
                    android:id="@+id/fooddescrition"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:lineSpacingMultiplier="1.5"
                    android:padding="12dp"
                    android:text="ful description"
                    android:textColor="@android:color/black"
                    android:textSize="14sp" />
            </androidx.cardview.widget.CardView>

            <LinearLayout 
                android:id="@+id/layoutprice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

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

                <TextView 
                    android:id="@+id/foodpricedetails"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="9"
                    android:layout_marginTop="10dp"
                    android:padding="12dp"
                    android:text="1000"
                    android:textColor="@color/colorPrimary"
                    android:textSize="18sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

内容的提问来源于stack exchange,提问作者Sideeg MoHammed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 15:17:48