简体   繁体   English

将imageview对齐到顶部

[英]Align imageview to top

I have JPGs of various sizes. 我有各种尺寸的JPG。

I created a custom listview with an imageview and two textviews. 我创建了一个带有imageview和两个textview的自定义listview。

I want to align the imageview to the top left of the LinearLayout, but nothing I have tried everything, but nothing works. 我想将imageview对齐到LinearLayout的左上角,但是什么也没有尝试,但是没有任何效果。

<?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">

    <ImageView
       android:id="@+id/petPhoto"
       android:layout_gravity="top|left"
       android:layout_width="150dp"
       android:layout_height="150dp" />

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

       <TextView
           android:id="@+id/petName"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />

       <TextView
           android:id="@+id/petDescription"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />

   </LinearLayout>

</LinearLayout>

Try this, 尝试这个,

Change your LinearLayout to RelativeLayout and add the alignParentTop property to top, 将LinearLayout更改为RelativeLayout并将alignParentTop属性添加到top,

<?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="wrap_content"
    android:padding="@dimen/card_padding"
    android:background="@color/card_background">

    <ImageView
        android:id="@+id/thumbnail"
        android:src="@drawable/thumbnail"
        android:layout_width="72dip"
        android:layout_height="72dip"
        android:scaleType="centerCrop"/>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Card title"
        android:layout_toRightOf="@+id/thumbnail"
        android:textAppearance="@android:style/TextAppearance.Holo.Medium"
        android:layout_marginLeft="?android:attr/listPreferredItemPaddingLeft"/>

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Card description"
        android:layout_toRightOf="@+id/thumbnail"
        android:layout_below="@+id/title"
        android:layout_marginLeft="?android:attr/listPreferredItemPaddingLeft"
        android:textAppearance="@android:style/TextAppearance.Holo.Small"/>

</RelativeLayout>

Output of your code like this, 这样的代码输出,

在此处输入图片说明

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

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