Question

I have a problem in my android layout.

I have an Image as following contains two parts:

  1. (green border) will contains a descriptive meaning
  2. (red border) I have to position a text in this part to be centered

I want the text to be in the bottom 1/3 of the image.

I have tried to make it programmatically but it doesn't work as I can't get (x, y, w, h) of an Image except it has rendered to the screen.

I have tried also the following code:

<RelativeLayout
                    android:id="@+id/item1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:layout_weight="1" >

                    <ImageView
                        android:id="@+id/item1_icon"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:adjustViewBounds="true"
                        android:src="@drawable/item1_icon" />

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:orientation="vertical"
                        android:weightSum="3" >

                        <LinearLayout
                            android:layout_width="fill_parent"
                            android:layout_height="0dp"
                            android:layout_weight="2" >
                        </LinearLayout>

                        <TextView
                            android:id="@+id/item1_title"
                            android:layout_width="fill_parent"
                            android:layout_height="0dp"
                            android:layout_weight="1"
                            android:ellipsize="end"
                            android:gravity="center"
                            android:maxLength="10"
                            android:singleLine="true"
                            android:text="My Day"
                            android:textColor="@android:color/white"
                            android:textStyle="bold" />
                    </LinearLayout>
                </RelativeLayout> 

How can I do that?

Was it helpful?

Solution

Note that the layout_weight attribute is assigned to the RelativeLayout:

<RelativeLayout
      android:id="@+id/item1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="5dp"
      android:layout_weight="1" >

It won't work this way. Only a LinearLayout can have a layout_weight attribute. For RelativeLayout it is ignored.

OTHER TIPS

I guess that you forgot to add this namespace to root view of your layout:

xmlns:android="http://schemas.android.com/apk/res/android"

And also I guess your problem will be solved if add it to your code.I added that to your code and try it.It was true.This is it's all:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:layout_weight="1" >

    <ImageView
        android:id="@+id/item1_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:weightSum="3"
         >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:background="@android:color/white" >
        </LinearLayout>

        <TextView
            android:id="@+id/item1_title"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:ellipsize="end"
            android:gravity="center"
            android:maxLength="10"
            android:singleLine="true"
            android:text="My Day"
            android:textColor="@android:color/white"
            android:textStyle="bold" />
    </LinearLayout>
</RelativeLayout>

This should work

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">

<ImageView
    android:id="@+id/item1_icon"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:src="@drawable/ic_launcher" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="@android:color/white" >
    </LinearLayout>

    <TextView
        android:id="@+id/item1_title"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLength="10"
        android:singleLine="true"
        android:text="My Day"
        android:textColor="@android:color/white"
        android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top