سؤال

Im trying to make a text view to align to the left border of another imageview in a RelativeLayout, but I'm having trouble and I cannot achieve this. Can anyone tell me how can I achieve this?

enter image description here

enter image description here

Here is my current XML layout:

<?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="wrap_content"
    android:layout_marginBottom="15dp"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/main_item_bg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:contentDescription="@string/app_name"
        android:scaleType="fitXY" />

    <RelativeLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/blackalha"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:weightSum="100" >

        <TextView
            android:id="@+id/main_item_cont"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/main_item_nmb"
            android:paddingLeft="5dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/white" />

        <ImageView
            android:id="@+id/main_item_ic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@color/app_purple"
            android:src="@drawable/bacal" />

        <TextView
            android:id="@+id/main_item_nmb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/main_item_ic"
            android:background="@drawable/saric"
            android:gravity="center"
            android:textColor="#333333" />

    </RelativeLayout>

    <TextView
        android:id="@+id/main_item_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/main_item_bg"
        android:layout_margin="5dp"
        android:maxWidth="200dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/white" />

</RelativeLayout>
هل كانت مفيدة؟

المحلول

Try this
<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/main_item_bg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:contentDescription="@string/app_name"
    android:scaleType="fitXY" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="80dip"
        android:gravity="center"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/main_item_cont"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:paddingLeft="5dp"
            android:text="dsgffdufsdfdfsdfdsfsdfsdfdsfdf"
            android:inputType="textMultiLine"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/white" />


        <ImageView
            android:id="@+id/main_item_ic"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dip"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@+id/main_item_cont"
            android:background="@android:color/black"
            android:src="@drawable/ic_launcher" />


        <TextView
            android:id="@+id/main_item_nmb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           android:layout_alignTop="@+id/main_item_cont"
            android:layout_toRightOf="@+id/main_item_cont"
            android:background="@android:color/white"
            android:text="10"
            android:textColor="@android:color/white" />
    </RelativeLayout>


<TextView
    android:id="@+id/main_item_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/main_item_bg"
    android:layout_margin="5dp"
    android:maxWidth="200dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/white" />

نصائح أخرى

As @Salauyou mentioned, try with android:layout_alignLeft because it "Makes the left edge of this view match the left edge of the given anchor view ID" and android:layout_toLeftOf means "Positions the right edge of this view to the left of the given anchor view ID."

Now, this is what I achieved:

enter image description here

I used this picture in drawable-mdpi (as the badge) - note the trick of the dual background:

enter image description here

and the standard launcher icon, since I didn't have a wine glass icon handy.

This is my layout:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:layout_margin="0dp"
    android:padding="0dp"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="0dp"
        android:padding="4dp"
        >
        <TextView
            android:id="@+id/txtIcon"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:padding="4dp"
            android:background="#6b2339"
            android:drawableRight="@drawable/ic_launcher"
        />
        <TextView
            android:id="@+id/txtBadge"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_toLeftOf="@id/txtIcon"
            android:gravity="center"
            android:padding="4dp"
            android:background="@drawable/wines_crossing_badge"
            android:text="8"
            android:textColor="#312c23"
            android:textSize="16sp"
        />
        <TextView
            android:id="@+id/txtDesc"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@id/txtBadge"
            android:background="#312c23"
            android:gravity="left|center_vertical"
            android:padding="4dp"
            android:text="Available Androids in stock for rental"
            android:textColor="#fff"
            android:textSize="16sp"
        />
    </RelativeLayout>
</RelativeLayout>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top