我希望在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" />更多推荐
发布评论