【问题标题】:white backgroud for png Image in android studioandroid studio中png图像的白色背景
【发布时间】:2020-01-20 23:18:17
【问题描述】:

我正在尝试在 RecyclerView 上显示一些 png 图像,图像具有透明背景,但在设备上运行时显示白色背景!!。 我尝试添加

android:background="@android:color/transparent"

到 ImageView,但仍然是白色

我使用 Picasso 将图像加载到 ImageView 上,就像那样

Picasso.get()
            .load(newpath)
                .into(itemView.imgMCat)

这是包含 RecyclerView 的片段

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="@drawable/meskback"


             tools:context=".HomeFragment">

    <!-- TODO: Update blank fragment layout -->

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

        <Toolbar
                android:id="@+id/toolbar"
                android:background="@color/meskBlue"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="?attr/actionBarSize"
                android:theme="?attr/actionBarTheme"
                android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                tools:targetApi="lollipop">

            <Button
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_gravity="right"
                    android:layout_margin="7dp"
                    android:id="@+id/mapiconbtn"
                    android:background="@drawable/mapicon"
            />

        </Toolbar>

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


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

                <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:id="@+id/mainActivityRelayout1"
                        android:paddingTop="10dp"
                        android:layout_weight="5">
                    <ImageView
                            android:layout_width="90dp"
                            android:layout_height="90dp"
                            tools:srcCompat="@drawable/logo"
                            android:id="@+id/logoimg"
                            android:contentDescription="@string/logoimgdes"
                            android:src="@drawable/logo"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="12dp"
                    />
                </RelativeLayout>

                <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:orientation="vertical"
                        android:layout_weight="5"
                        android:weightSum="2">
                    <TextView
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:textSize="22sp"
                            android:layout_weight="1"
                            android:textStyle="bold"
                            android:gravity="center"
                            android:textColor="@color/meskBlue"
                            android:text="@string/welcome"
                            android:textAlignment="center"/>
                    <TextView
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="1"
                            android:textSize="20sp"
                            android:textStyle="bold"
                            android:gravity="center"
                            android:layout_marginTop="20dp"
                            android:text="@string/how_can_we_help_you"
                            android:textAlignment="center"/>

                    />
                </LinearLayout>

            </LinearLayout>
        </LinearLayout>

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

        <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/McatRecylceView"/>

    </LinearLayout>

    </LinearLayout>


</FrameLayout>

这是包含 ImageView 的 item_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="10dp">


    <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="140dp">

        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            <ImageView
                    android:id="@+id/imgMCat"
                    android:layout_width="match_parent"
                    android:layout_height="80dp"
                    android:background="@android:color/transparent"
                    android:layout_centerInParent="true"
                    android:layout_marginTop="3dp" android:layout_marginStart="3dp"
                    android:layout_marginEnd="3dp" android:layout_marginBottom="3dp"/>
            <TextView
                    android:text="TextView"
                    android:id="@+id/mcategoryTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/imgMCat"
                    android:textAlignment="center"/>

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

</LinearLayout>

此设备照片

【问题讨论】:

  • 您能分享您的完整 XML 文件吗?
  • 这也取决于您在 AndroidManifest.xml 中为 Activity 设置的内容。还要检查您是否没有更改类内代码中的背景颜色。
  • @TaslimOseni 我加了,请查收
  • @MikeM。如果你的意思是 png 图片,这是miskaldar.com/uploadimg/mcategory/1547760850.png
  • 从图片上很难看出来:CardView还有什么别的吗?该卡可以提供纯色/不透明背景。

标签: android kotlin


【解决方案1】:

试试这个

<android.support.v7.widget.CardView
        android:scaleType="centerInside"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:elevation="12dp"
        android:shape="ring"
        app:cardCornerRadius="17dp">

        <ImageView
            android:id="@+id/avatar"
            android:layout_width="49dp"
            android:layout_height="49dp"
            android:layout_marginLeft="-8dp"
            android:layout_marginTop="-5dp"
            android:src="@drawable/blank" />

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

【讨论】:

    【解决方案2】:

    图像加载没有问题,你需要做的是设置高度和宽度来包装内容,而不是固定宽度和匹配父高度,这是你应该在 xml 中做的事情

    <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="80dp">
    
            <ImageView
                    android:id="@+id/imgMCat"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:layout_centerInParent="true"
                    android:layout_marginTop="3dp" android:layout_marginStart="3dp"
                    android:layout_marginEnd="3dp" android:layout_marginBottom="3dp"/>
            <TextView
                    android:text="TextView"
                    android:id="@+id/mcategoryTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/imgMCat"
                    android:textAlignment="center"/>
    
        </RelativeLayout>
    

    通过将主布局设置为您想要的大小,然后设置 imageview 以包装 png 的内容,您将解决您的问题

    但是,我建议您永远不要对任何 UI 组件使用固定尺寸,因为具有不同屏幕尺寸的不同设备数量众多,如果您使用固定尺寸的 UI 元素,那么它肯定会在 1 个屏幕或其他

    【讨论】:

      【解决方案3】:

      我在这里找到了答案,问题来自 CardView https://stackoverflow.com/a/45769769/2209611

      我添加了这些属性并修复了问题

      app:cardBackgroundColor="@android:color/transparent"
              app:cardElevation="0dp"
      

      【讨论】:

      • 如果你想要没有背景和阴影,你为什么首先使用 CardView?圆角可能是原因之一,但如果您不想或不关心圆角,那么我建议您改用FrameLayout
      猜你喜欢
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 2015-03-02
      • 2021-03-28
      相关资源
      最近更新 更多