简体   繁体   English

自定义listview中的Android图像高度

[英]Android image height in custom listview

I have a listview with custom rows. 我有一个带有自定义行的列表视图。 In imageView1 I set an image, but it is smaller than the height of the row . 在imageView1中,我设置了一个图像,但是它小于row的高度。 The image size is 4x30px. 图片大小为4x30px。 When I set a 40x300 img size (programmatically), the row gets too big (I know the reason: wrap_content). 当我(以编程方式)设置40x300 img大小时,行变得太大(我知道原因:wrap_content)。 How can I set that the image be streched in height to the height of the row? 如何设置将图像的高度拉伸到行的高度?

holder.image.setImageDrawable(convertView.getResources().getDrawable(R.drawable.priority_4x30));

xml.file: xml.file:

   <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
    >
      <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon"
        android:focusable="false" />

      <TextView android:id="@+id/textView1" 
        android:layout_width="180dip" 
        android:layout_height="wrap_content"
        android:paddingTop="15dip"
        android:paddingBottom="15dip"
        android:textColor="#000000"
        android:layout_toRightOf="@+id/imageView1">
      </TextView>

      <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon"
        android:layout_toRightOf="@+id/textView1" />

      <CheckBox android:id="@+id/checkBox1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="6sp"
        android:focusable="false"
        android:layout_toRightOf="@+id/imageView2">
      </CheckBox>

    </RelativeLayout>

You should make your relativelayout for each item have a static height 您应该使每个项目的relativelayout具有静态高度

ex: 例如:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight">

and then you can simply change your imageview to fill parent for its height 然后您可以简单地更改imageview以使其高度填充父级

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:src="@drawable/icon"
    android:layout_toRightOf="@+id/textView1" />

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

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