ImageView不会填充其布局的高度(ImageView doesn't fill the height of its layout)

编程入门 行业动态 更新时间:2024-10-28 22:26:35
ImageView不会填充其布局的高度(ImageView doesn't fill the height of its layout)

我希望在ListView添加一个ImageView 。 我希望图像能够在没有扭曲的情况下填充布局(基本上,填充高度并调整其宽度)。

这就是我现在得到的:

我为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())但它也失败了。

谁能指出我出了什么问题?

非常感谢

I wish to add an ImageView inside a ListView. I would like the image to fill the layout without distorsion (basically, fill the height and adapt its width).

This is what I get now:

I have put a grey background to the ImageView to make it easier to see. In red, it's the size I wish to get.

Here is my code:

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>

method getView() of my Adapter :

@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; }

I tried to play with different parameters such as ScaleType, android:adjustViewBounds and layout_width but the height of my image never changed. I also tried to do it programatically (something like image.setMinHeight(parent.getHeight()) but it failed too.

Could anyone point me out what is going wrong?

Many thanks

最满意答案

尝试将layout_height的RelativeLayout更改为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>

或者你可以将ImageView的minHeight属性设置为?android:attr/listPreferredItemHeight

<ImageView ... android:minHeight="?android:attr/listPreferredItemHeight" />

Try changing the RelativeLayout of the layout_height to 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>

Alternatively you could set the minHeight attribute of the ImageView to ?android:attr/listPreferredItemHeight

<ImageView ... android:minHeight="?android:attr/listPreferredItemHeight" />

更多推荐

本文发布于:2023-08-03 01:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1383207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布局   高度   ImageView   layout   height

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!