How to align ImageView right

时间:2016-04-15 15:02:17

标签: java android listview android-studio

I have a list of insurance companies using a listView and I'm trying to align my arrow images to the right. I have looked at several examples but none seem to work. My code is as fallows

myList.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#26BFDA">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:padding="5dp" />



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

        <TextView
            android:id="@+id/item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:textColor="#FFFFFF"

            />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>

    <ImageView
        android:layout_width="20dp"
        android:layout_height="wrap_content"
        android:id="@+id/imageView2"
        android:layout_weight="0.1"
        android:layout_alignParentRight="true"
        android:src="@drawable/nextarrow"/>
</LinearLayout>

Main Activity

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    >
</ListView>

The image shows how it currently looks like. enter image description here

3 个答案:

答案 0 :(得分:1)

Use layout_weight LinearLayout property

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/professionel_ll_item_professionel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="10"
    android:orientation="horizontal"
    >
  <LinearLayout
      android:layout_width="0dp"
      android:layout_height="100dp"
      android:layout_weight="2"
      android:gravity="center"
      >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:padding="5dp"
        />

  </LinearLayout>

  <LinearLayout
      android:layout_width="0dp"
      android:layout_height="fill_parent"
      android:layout_weight="6"
      android:gravity="center_vertical"
      android:orientation="vertical"

      >

    <TextView
        android:id="@+id/item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:padding="2dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF"

        />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="TextView"
        />


  </LinearLayout>
  <LinearLayout
      android:layout_width="0dp"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:gravity="center"
      android:orientation="vertical"
      >
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="20dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/nextarrow"
        />
  </LinearLayout>
</LinearLayout>

答案 1 :(得分:1)

This should do it

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#26BFDA">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:padding="5dp" />



    <LinearLayout android:layout_width="wrap_content"
        android:toRightof="@id/icon"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/itemContainer"
        >

        <TextView
            android:id="@+id/item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:textColor="#FFFFFF"

            />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>

    <ImageView
        android:layout_width="20dp"
        android:layout_height="wrap_content"
        android:id="@+id/imageView2"
        android:layout_alignParentRight="true"
        android:src="@drawable/nextarrow"/>
</RelativeLayout>

答案 2 :(得分:0)

you can add the rest weight to the other view in order to summarize 1. Try adding android:layout_weight="0.9" to the vertical-orientation LinearLayout.