简体   繁体   中英

Align two imageView with textView XML in android

I have a problem with my xml for android. I have this layout to use it in each line of a Listview (that works fine).

I want to put two lines (head and body) of text, and align Right of the first line I have to put two ImageView. (Also have first of all a hidden textView)

Something like this:

My problem is when I try to put both imageViews (with only one it shows fine, but with two don't show nothing).

this is my xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/TvIdGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textStyle="bold"
        android:visibility="gone" />

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

        <TextView
            android:id="@+id/TvName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:textSize="30sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/ImgNewConvGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:src="@drawable/new_messages" />

        <ImageView
            android:id="@+id/ImgNewGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:src="@drawable/new_group" />
    </LinearLayout>

    <TextView
        android:id="@+id/TvDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:textStyle="normal" />

</LinearLayout>

sorry for my english and thank you in advance!

If I am understanding correctly then I believe this should be close to what you are looking for:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/TvIdGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test"
        android:textSize="12sp"
        android:textStyle="bold"
        android:visibility="gone" />

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

        <TextView
            android:id="@+id/TvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Test"
            android:textSize="30sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/ImgNewConvGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:gravity="right"
            android:src="@drawable/new_messages" />

        <ImageView
            android:id="@+id/ImgNewGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/ImgNewConvGroup"
            android:gravity="right"
            android:src="@drawable/new_group" />
    </RelativeLayout>

    <TextView
        android:id="@+id/TvDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test"
        android:textSize="22sp"
        android:textStyle="normal" />

</LinearLayout>

Try it out and let me know if that works for you, I changed the inner layout from LinearLayout to RelativeLayout and created what I believe you are looking for in relational locations.

Note: Be cautious of your TextView extending far enough to go behind the ImageViews , if that happens then it might be better to (set for the ImageViews ):

android:layout_below="@+id/TvName"

on both and remove:

android:layout_centerVertical="true"

<TextView
    android:id="@+id/TvIdGroup"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="TV"
    android:textSize="12sp"
    android:textStyle="bold"
    android:visibility="visible" />

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

    <TextView
        android:id="@+id/TvName"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_weight="1"
        android:text="TVName"
        android:textSize="30sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/ImgNewConvGroup"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:gravity="right"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/ImgNewGroup"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:gravity="right"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

<TextView
    android:id="@+id/TvDescription"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TVDescription"
    android:textSize="22sp"
    android:textStyle="normal" />

Take a look if is what are you looking for.

Is a lot of code just will write the structure

<LinearLayout android:orientation="vertical">
    <LinearLayout android:orientation="horizontal">
        <LinearLayout android:orientation="horizontal">
           <TextView/>
        </LinearLayout>
        <LinearLayout android:orientation="horizontal" android:gravity="right">
            <ImageView/>
            <ImageView/>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout android:orientation="vertical">
        <TextView/>
    </LinearLayout>
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM