ListView 项目中的图像未填充高度
Images in ListView items not filling height
创建列表视图后,每个列表项的一侧都有 XML 个可绘制对象,由于某些原因,图像视图未按预期方式显示。对于每个项目,只有一条小水平线出现在每个列表项的顶部,而不是 drawable 也填满每个列表视图项目的高度。需要做什么来固定图片的高度?
drawable/ic_red.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="20dp" />
<solid android:color="@color/red"/>
</shape>
drawable/ic_yellow.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="20dp" />
<solid android:color="@color/yellow"/>
</shape>
drawable/ic_green.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="20dp" />
<solid android:color="@color/green"/>
</shape>
fragment_helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_helloworld"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
listitem_helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/item_img"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingStart="5dp"
android:paddingEnd="5dp"/>
<TextView
android:id="@+id/item_colourtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
android:layout_toEndOf="@id/item_img"/>
</RelativeLayout>
FragmentHelloWorld.java
public class FragmentHelloWorld extends android.support.v4.app.Fragment {
public FragmentHelloWorld() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_helloworld, container, false);
// Array of strings for ListView
String[] listviewTitle = new String[]{
"Item 1",
"Item 2",
"Item 3"
};
// Array of images for ListView
int[] listviewImage = new int[]{
R.drawable.ic_red,
R.drawable.ic_yellow,
R.drawable.ic_green
};
List<HashMap<String, String>> aList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
HashMap<String, String> hm = new HashMap<>();
hm.put("listview_title", listviewTitle[i]);
hm.put("listview_image", Integer.toString(listviewImage[i]));
aList.add(hm);
}
String[] from = {"listview_image", "listview_title"};
int[] to = {R.id.item_img, R.id.item_colourtitle};
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listitem_helloworld, from, to);
ListView list_helloworld = (ListView) v.findViewById(R.id.list_helloworld);
list_helloworld.setAdapter(simpleAdapter);
}
你可能想看看这个posthere
或者您可以尝试为可绘制对象指定一个矩形并使用 LinearLayout
,那会很简单。
创建列表视图后,每个列表项的一侧都有 XML 个可绘制对象,由于某些原因,图像视图未按预期方式显示。对于每个项目,只有一条小水平线出现在每个列表项的顶部,而不是 drawable 也填满每个列表视图项目的高度。需要做什么来固定图片的高度?
drawable/ic_red.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="20dp" />
<solid android:color="@color/red"/>
</shape>
drawable/ic_yellow.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="20dp" />
<solid android:color="@color/yellow"/>
</shape>
drawable/ic_green.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="20dp" />
<solid android:color="@color/green"/>
</shape>
fragment_helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_helloworld"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
listitem_helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/item_img"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingStart="5dp"
android:paddingEnd="5dp"/>
<TextView
android:id="@+id/item_colourtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
android:layout_toEndOf="@id/item_img"/>
</RelativeLayout>
FragmentHelloWorld.java
public class FragmentHelloWorld extends android.support.v4.app.Fragment {
public FragmentHelloWorld() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_helloworld, container, false);
// Array of strings for ListView
String[] listviewTitle = new String[]{
"Item 1",
"Item 2",
"Item 3"
};
// Array of images for ListView
int[] listviewImage = new int[]{
R.drawable.ic_red,
R.drawable.ic_yellow,
R.drawable.ic_green
};
List<HashMap<String, String>> aList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
HashMap<String, String> hm = new HashMap<>();
hm.put("listview_title", listviewTitle[i]);
hm.put("listview_image", Integer.toString(listviewImage[i]));
aList.add(hm);
}
String[] from = {"listview_image", "listview_title"};
int[] to = {R.id.item_img, R.id.item_colourtitle};
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listitem_helloworld, from, to);
ListView list_helloworld = (ListView) v.findViewById(R.id.list_helloworld);
list_helloworld.setAdapter(simpleAdapter);
}
你可能想看看这个posthere
或者您可以尝试为可绘制对象指定一个矩形并使用 LinearLayout
,那会很简单。