图像未在UI中显示

时间:2011-04-30 18:47:41

标签: android imageview android-xml

我在3个文件夹中有一个图像res / drawable-hdpi / mdpi / ldpi 600 * 600作为分辨率)我有这个XML文件来显示textview和图像:

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

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/sipLabel"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  />
  <ImageView android:id="@+id/connected" android:src="@drawable/connected" android:layout_below="@id/sipLabel" 
  android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_weight="0.35" android:gravity="center" 
         />
        </LinearLayout>

问题可能是什么? 谢谢你的帮助。

3 个答案:

答案 0 :(得分:2)

主要问题是LinearLayout中的第一个标记:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/sipLabel"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  />

由于layout_height设置为fill_parent,因此TextView会垂直填充LinearLayout,不会为图像留下空间。尝试将layout_height更改为wrap_content

另外,还有其他一些事情:

  • 您正在使用android:layout_below="@id/sipLabel",但这仅适用于RelativeLayout。所以这个属性被默默地忽略了。
  • 虽然您可以选择任何layout_weight,但0.35非常随意。由于它是LinearLayout中唯一具有权重的子项,因此它将接收所有额外的垂直空间。
  • 您无需在TextView代码中加入xmlns:android="http://schemas.android.com/apk/res/android"

答案 1 :(得分:1)

我认为layout_below仅适用于RelativeLayouts。此外,layout_weight="0.35"看起来很可疑,我认为这并不意味着你的意思。我认为它必须有一个整数值。

答案 2 :(得分:1)

由于TextView的高度为fill-parent,而LinearLayout未滚动(除非放入ScrollView,否则无法看到下方部分) ,您的TextView占据了活动的整个屏幕,其下的ImageView不可见。

所以你可以

  • 将您的整个LinearLayout放入 ScrollView,然后向下滚动查看 你的形象,或
  • 如果您的目标是显示图像 屏幕的底部,和 应该采取上面的整个地方 是TextView,然后是 `RelativeLayout将是最好的 选项。

<强>更新
一个有效的RelativeLayout解决方案

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:id="@+id/sipLabel" android:text="@string/loremipsum1"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />
    <ImageView android:id="@+id/connected" android:src="@drawable/connected"
        android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:layout_below="@id/sipLabel" android:layout_alignParentBottom="true" />
</RelativeLayout>