Android-ImageView 未显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10774848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:55:05  来源:igfitidea点击:

Android- ImageView not showing

androidimageviewgravity

提问by Lijap

I have this code:

我有这个代码:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout02"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >   


 <ImageView  android:id="@+id/terranlogo1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/terranlogo"
    android:layout_centerHorizontal="true"
    />

 <ImageView  android:id="@+id/protosslogo1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:gravity="top|right"
    android:src="@drawable/protosslogo"
    />

 <ImageView  android:id="@+id/zergologo1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:gravity="top|left" 
    android:src="@drawable/zerglogo"
    />

The image view on the right is not showing. The other 2 are just fine, I even tried setting them to 50 dp and the "protosslogo1" still does not show. What am I doing wrong?

右侧的图像视图未显示。其他 2 个都很好,我什至尝试将它们设置为 50 dp,但“protosslogo1”仍然没有显示。我究竟做错了什么?

Thanks, All help is appreciated- Lijap

谢谢,感谢所有帮助- Lijap

回答by Luksprog

Position your ImageViewslike this:

ImageViews像这样定位:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout02"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >   


 <ImageView  android:id="@+id/terranlogo1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/terranlogo"
    android:layout_centerHorizontal="true"
    />

 <ImageView  android:id="@+id/protosslogo1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentRight="true"
    android:src="@drawable/protosslogo"
    />

 <ImageView  android:id="@+id/zergologo1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true" 
    android:src="@drawable/zerglogo"
    />

Right now protosslogo1is behind zergologo1. You're using android:gravitythat is used to place the content(and the size of your ImageViewis fixed so it doesn't matter) of the View. Second, you don't have any rules set on your ImageViewsso they are placed by default(in a RelativeLayout) in the top-left corner.

现在protosslogo1是落后zergologo1。您使用android:gravity的是用于将内容放置(和你的大小ImageView是固定的,因此并不重要)的View。其次,您没有设置任何规则,ImageViews因此默认情况下(在 a 中RelativeLayout)将它们放置在左上角。

回答by Gouda Elalfy

you can use:

您可以使用:

android:background="@drawable/terranlogo"

instead of:

代替:

android:src="@drawable/terranlogo"