Image not showing in the UI
-
27-10-2019 - |
سؤال
I have an image in the 3 folders res/drawable-hdpi/mdpi/ldpi 600*600 as resolution) and i have this XML file to show a textview and the image:
<?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>
What could br the problem? Thank you for your help.
المحلول
The main issue is your first tag in the 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"
/>
Since layout_height
is set to fill_parent
this TextView is filling the LinearLayout vertically, leaving no room for the image. Try changing layout_height
to wrap_content
.
Also, a couple of other things:
- You're using
android:layout_below="@id/sipLabel"
, but this only works in a RelativeLayout. So this attribute is being silently ignored. - While you can choose any
layout_weight
you want,0.35
is pretty arbitrary. Since it's the only child in the LinearLayout with weight, it will receive all of the extra vertical space. - You don't need to include the
xmlns:android="http://schemas.android.com/apk/res/android"
on your TextView tag.
نصائح أخرى
I think layout_below
only applies for RelativeLayouts. Also, that layout_weight="0.35"
looks mighty suspect, I don't think it means what you think it means. I think it must have an integer value.
Since your TextView
has a height of fill-parent
, and the LinearLayout
is not scrolling (unless is put into a ScrollView
you can't see the lower parts), your TextView
takes up the whole screen of the Activity, and the ImageView
being under it is not visible.
So you can either
- put your whole
LinearLayout
into aScrollView
, and scroll down to see your image, or - if your goal is to show the image at
the bottom of the screen, and the
whole place above it should be taken
be the
TextView
, then a `RelativeLayout would be the best option.
Update
a working RelativeLayout
soulution would be
<?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>