ImageView无法填充其布局的高度。

3
我希望在一个ListView中添加一个ImageView。 我希望图像能够填满布局而不失真(基本上,填充高度并调整其宽度)。
这是我现在的效果: enter image description here 我给ImageView加了一个灰色背景,以便更容易看到。 红色标记出我想要的尺寸。
以下是我的代码: Project_form.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

     <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/name_project"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:padding="3dp"
        android:layout_alignParentLeft="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="25dp" >
    </TextView>

    <Button
        android:id="@+id/button_project"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_custom"
        android:layout_centerVertical="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_alignParentRight="true"
        android:padding="3dp"
        android:text="  details  "
        android:textSize="20dp"
        android:visibility="invisible" />

    <ImageView
        android:id="@+id/imagesstep"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="@color/grey"
        android:adjustViewBounds="false"
        android:layout_centerVertical="true"
        android:padding="3dp"
        android:layout_alignParentRight="true"
        android:visibility="invisible" />
 </RelativeLayout>

Adapter中的getView()方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {            
    View row = convertView;            
    if (row==null) {       
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        row = inflater.inflate(R.layout.project_form, parent, false);
    }   
    TextView text = (TextView) row.findViewById(R.id.name_project); 
    text.setText(this.objects[position].getName());       
    int IDImage=this.objects[position].getIDImage();       
    int IDPlay = this.objects[position].getIDPlay();
    text.setCompoundDrawablesWithIntrinsicBounds(IDImage, 0, 0, 0);         
    text.setCompoundDrawablePadding(7);    
    ImageView image = (ImageView)
    row.findViewById(R.id.imagesstep);                
    image.setVisibility(View.VISIBLE);          
    image.setImageResource(IDPlay);
    if (position == selectedPosition) {
        row.setBackgroundColor(Color.parseColor("#8000ff00"));
    }  
    else {
        row.setBackgroundColor(Color.TRANSPARENT);
    }

    return row;
}

我尝试使用不同的参数,如ScaleType,android:adjustViewBounds和layout_width,但我的图像高度从未改变。我还尝试以编程方式实现它(类似于image.setMinHeight(parent.getHeight()),但它也失败了。
有人能指出我做错了什么吗?
非常感谢。
2个回答

2
尝试将 layout_heightRelativeLayout 更改为 fill_parent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent">

    <TextView ... />

    <Button .. />

    <ImageView ... />
 </RelativeLayout>

或者您可以将ImageViewminHeight属性设置为?android:attr/listPreferredItemHeight


该方法可用于调整ImageView的最小高度,使其与列表项高度相同。
<ImageView
    ...
    android:minHeight="?android:attr/listPreferredItemHeight" />

1
谢谢您的回答。将宽度和高度都设置为"?android:attr/listPreferredItemHeight"就解决了问题。 - Gordak

1

从您的ImageView中移除填充


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接