2015-10-30 125 views
0

我有各种尺寸的JPG。将imageview对齐顶部

我创建了一个带有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> 

回答

0

试试这个,

你的LinearLayout更改的RelativeLayout并添加alignParentTop财产顶部,你的代码是这样的

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

输出,

enter image description here

+0

不,不行。 imageview现在出现在中心。它也没有被列在listview的每一行的顶部。我想要左边的图像视图 - 对齐左上角和右上角。 – user2923955

+0

我已经编辑了imageview,请检查这一点,我已经插入了属性,alignParentLeft =“true” –

+0

为了更好的参考,请分享你的照片,你需要它,所以我可以按照快照指导你。 –