在listview中调整android图像大小
我有一个相对布局中的ImageView,它具有以下属性android:layout\u width=“wrap\u content”android:layout\u height=“wrap\u content” 在适配器的帮助下,我将此布局填充到mainactivity中的ListItem中。然后我尝试用下面的代码在适配器中调整大小在listview中调整android图像大小,android,resize,imageview,Android,Resize,Imageview,我有一个相对布局中的ImageView,它具有以下属性android:layout\u width=“wrap\u content”android:layout\u height=“wrap\u content” 在适配器的帮助下,我将此布局填充到mainactivity中的ListItem中。然后我尝试用下面的代码在适配器中调整大小 DisplayMetrics dm = new DisplayMetrics(); dm = ctx .getResources().getDisplayMet
DisplayMetrics dm = new DisplayMetrics(); dm = ctx .getResources().getDisplayMetrics(); int width = dm.widthPixels; int height = width * 243 / 325;
RelativeLayout.LayoutParams layoutParams = new
RelativeLayout.LayoutParams(width, height); thumbImage.setLayoutParams(layoutParams);
但是,这些图像会卡在listview项中,甚至比其原始大小还要小
包含在适配器中填充的imageview的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="?android:attr/listPreferredItemHeight" android:padding="0dp">
<TextView
android:id="@+id/tvKategori"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
/>
<ImageView
android:id="@+id/ivThumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</relativelayout>
这是包含listview的主要活动xml,我填充了lvgunuvideolari listview和lvencokizlenner内容
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!-- Listview to display slider menu -->
<ExpandableListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="0px"
android:paddingLeft="0px"
android:paddingRight="0px"
android:paddingTop="0px" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="0px"
android:paddingLeft="0px"
android:paddingRight="0px"
android:paddingTop="0px"
android:weightSum="1.0" >
<Button
android:id="@+id/btnGununVideolari"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Günün Videoları"
android:textSize="12sp" />
<Button
android:id="@+id/btnEnCokIzlenenler"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="En Çok İzlenenler"
android:textSize="12sp" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/lvGununVideolari"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="0px"
android:paddingLeft="0px"
android:paddingRight="0px"
android:paddingTop="0px" >
</ListView>
<ListView
android:id="@+id/lvEnCokIzlenenler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingBottom="0px"
android:paddingLeft="0px"
android:paddingRight="0px"
android:paddingTop="0px" >
</ListView>
</RelativeLayout>
</linearlayout> </android.support.v4.widget.drawerlayout>
您需要设置图像缩放的方式,如:
<ImageView
android:id="@+id/ivThumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="false" />
但是,在代码中通过LayoutParams执行的操作基本相同:)它不起作用,listview中的项是否有特定的高度大小。这是fitxy的新screetshot:实际上它按预期工作。唯一的问题是宽度。尝试用LayoutParams注释代码,并设置50dp的宽度和高度,如我的第二部分所述。同时尝试设置android:adjustViewBounds=“true”。正如您在屏幕截图上看到的,所有图像的高度和宽度都相同,但宽度太大。我删除了适配器中的布局参数代码。这里是屏幕截图:是的,再次说明一切都很好,工作正常。你的问题是关于调整大小,你明白了。您在问题中写道,但是图像会被困在listview项中,甚至比原始大小还要小。现在,图像不再是更小或更大——它们都具有相同的尺寸。就像我说的“还有其他的选择可用”的缩放方式。尝试其他选项并了解其工作原理。ops我发现问题:android:layout_height=“?android:attr/listPreferredItemHeight”
<ImageView
android:id="@+id/ivThumbnail"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:adjustViewBounds="false" />