【问题标题】:Imageview doesn't displays when I set background color当我设置背景颜色时,Imageview 不显示
【发布时间】:2013-11-30 18:46:18
【问题描述】:

XML文件的结构是:

FrameLayout 
    ImageView
    LinearLayout
        TextView
        TextView

当我为 LinearLayout 使用背景颜色时,ImageView 的可绘制对象不会出现。如果我从 LinearLayout 可绘制对象中删除 bg 颜色就在它的位置。

我正在这样的代码中添加可绘制对象:

              if( placetype == 0 )  {
                    ImageView im0 = (ImageView) myContentsView.findViewById(R.id.infowindow_imageview);
                    int imageResource0 = R.drawable.beer_white;
                    im0.setImageResource(imageResource0);
                } else if( placetype == 1 ) { 
                     //etc ... 

还有 XML 代码:

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

<FrameLayout
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?activatableItemBackground"
    android:id="@+id/map_info_top"  >     
          <ImageView
              android:id="@+id/infowindow_imageview"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="top|right"
              android:layout_marginRight="-12dp"
              android:layout_marginTop="-12dp"
              android:adjustViewBounds="true"
              android:alpha="0.3"
              android:contentDescription="@null"
              android:scaleType="fitXY" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/red_drink"
                android:orientation="vertical" >

                    <TextView
                        style="@style/TextHeader"
                        android:id="@+id/custom_infowindow_title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textStyle="bold"

                        android:paddingLeft="8dp"
                        android:paddingRight="8dp"
                        android:paddingBottom="8dp"     />

                    <TextView
                        style="@style/TextHeaderSub"
                        android:text="Click to Read more"                           
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:paddingLeft="8dp"
                        android:paddingRight="8dp"
                        android:paddingBottom="8dp"
                         />

            </LinearLayout>     

</FrameLayout>


</LinearLayout>

由于 ImageView 和 LinearLayout 在 FrameLayout 中,我不认为重叠视图会有问题。 有人可以解释一下我在这里做错了什么吗?

【问题讨论】:

    标签: android xml android-layout


    【解决方案1】:

    您的 LinearLayout 被绘制在 ImageView 上。而且由于背景不透明,因此您看不到图像。尝试将 ImageView 放在 XML 资源中 LinearLayout 的下方

    【讨论】:

    • @mt0s 因为您使用的是 FrameLayout。
    • @JaAd 是的,我的错误 :( FrameLayout is designed to block out an area on the screen to display a **single** item.
    • 这就是 FrameLayout 的工作原理。它把每个孩子都放在了以前所有的孩子之上。不在下面,就像在简单的 LinearLayout 中一样。 FrameLayout 就像您使用 RelativeLayout 并忘记为每个孩子设置 android:below 之类的参数或设置 alignParentTop="true"
    • 非常感谢。自从我上次使用 FrameLayout 已经有一段时间了,我忘记了。再给我两分钟,将答案标记为已接受。
    猜你喜欢
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 2012-10-07
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2015-01-17
    相关资源
    最近更新 更多