简体   繁体   中英

Align ImageView to top right in a LinearLayout

This is what I want:

[screenshot]

在此处输入图片说明

Here is what I have done:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="30dip"
    android:background="@color/color"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="1dp"
        android:background="@color/color"
        android:orientation="horizontal">

        <View
            android:id="@+id/divider"
            android:layout_width="5dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:background="@color/grey"
            android:gravity="center_vertical" />


    <LinearLayout
        android:id="@+id/llayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/color"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/label"
            style="@style/some_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginLeft="8dp"
            android:gravity="left" />

        <TextView
            android:id="@+id/label2"
            style="@style/some_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginLeft="8dp"
            android:gravity="left" />

    </LinearLayout>

    </LinearLayout>

</merge>

I have done almost, but the problem is how to align image(can be seen in the screenshot) on the top right of a vertical linearlayout.

Also I have tried using Relative Layout, Image is coming on the top right,but it is cutting my text in the textview.

Please suggest:

LinearLayut放置另一个RelativeLayout ,其中包含您的ImageView并应用于ImageView属性android:layout_alignParentRight="true"

You can set it by layout_gravity property of imageview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.fierbasedemo.MainActivity">

    <ImageView
        android:id="@+id/tv_name"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="right" />
</LinearLayout>

android:layout_gravity="right"到 imageview 的父布局

You need to put ImageView as Top element of LinearLayout and give its gravity as:

android:layout_gravity="right|end"

If possible give static size to that ImageView.

In my case solved inverting. Just put a LinearLayout inside your RelativeLayout and set your image on your RelativeLayout only.

For me did the trick with this settings.

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

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@layout/list_bg_selector">

            <ImageView
                android:id="@+id/book_img_id"
                android:layout_width="match_parent"
                android:layout_height="110dp"
                android:scaleType="centerCrop"/>

            <TextView
                android:id="@+id/book_title_id"
                android:textColor="@color/azul_band"
                android:textSize="10sp"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Book Title"/>
        </LinearLayout>
        <ImageView
            android:id="@+id/imgFavorito"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_alignParentRight="true"
            android:layout_gravity="right|end"
            android:src="@drawable/bookmark_off"
            android:layout_marginRight="2dp"
            android:paddingTop="3dp"/>
    </RelativeLayout>

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