Can't alight TextView toRightOf ImageView

时间:2015-06-26 09:30:12

标签: android android-layout

The TextView doesn't alight to right (end) of ImageView for some reason. See battery image and its percents.

Current:

enter image description here

Expected:

enter image description here

The code:

...
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1.3">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:scaleType="fitStart"
        android:id="@+id/battery_level"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_toRightOf="@id/battery_level"
        android:layout_toEndOf="@id/battery_level"
        android:layout_alignEnd="@id/battery_level"
        android:layout_alignRight="@id/battery_level"
        android:layout_marginLeft="6dp"
        android:layout_marginStart="6dp"
        android:id="@+id/battery_level_percents"
        style="@style/Blue_TextViewStyle"
        />
</RelativeLayout>
...

Any idea what causes that behaviour?


EDIT: If I hardcode image source to ImageView and text to TextView then it looks like in Design view:

enter image description here

the code of it:

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:scaleType="fitStart"

Seems like even wrap_content doesn't work.

5 个答案:

答案 0 :(得分:1)

Try to add android:adjustViewBounds="true" to your ImageView.

The Bounds might be to big if the source picture was larger.

答案 1 :(得分:0)

Use this:

<ImageView
    android:id="@+id/battery_level"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:scaleType="fitStart"
    android:src="@drawable/ic_action_next_item" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/battery_level"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@+id/battery_level"
    android:layout_toRightOf="@+id/battery_level"
    android:text="New Text" />

答案 2 :(得分:0)

   <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageView1"
        android:text="TextView" />
</RelativeLayout>

答案 3 :(得分:0)

You Can Use Like This.

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/battery_level"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitStart" />

    <TextView
        android:id="@+id/battery_level_percents"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</LinearLayout>

答案 4 :(得分:0)

Try this:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="TextView" />
</RelativeLayout>