简体   繁体   English

在 LinearLayout 中将 ImageView 对齐到右上角

[英]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您可以通过 imageview 的 layout_gravity 属性设置它

<?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:您需要将 ImageView 作为 LinearLayout 的 Top 元素并将其重力设置为:

android:layout_gravity="right|end"

If possible give static size to that ImageView.如果可能,请为该 ImageView 提供静态大小。

In my case solved inverting.在我的情况下解决了反转。 Just put a LinearLayout inside your RelativeLayout and set your image on your RelativeLayout only.只需在您的 RelativeLayout 中放置一个 LinearLayout 并仅在您的 RelativeLayout 上设置您的图像。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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