Java ListView项目中的图像未填充高度

Java ListView项目中的图像未填充高度,java,android,xml,listview,android-drawable,Java,Android,Xml,Listview,Android Drawable,在创建了一个列表视图(每个列表项的侧面都有XML可绘制内容)之后,由于某些原因,图像视图的显示方式与预期不同。对于每个项目,在每个列表项目的顶部仅显示一条小的水平线,而不是填充每个列表视图项目高度的可绘制线。为了固定图像的高度,需要做什么 drawable/ic_red.xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/andro

在创建了一个列表视图(每个列表项的侧面都有XML可绘制内容)之后,由于某些原因,图像视图的显示方式与预期不同。对于每个项目,在每个列表项目的顶部仅显示一条小的水平线,而不是填充每个列表视图项目高度的可绘制线。为了固定图像的高度,需要做什么

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>
<?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>
<?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>
<?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>
<?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>

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/red"/>
</shape>
<?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>
<?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>
<?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>
<?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>

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/red"/>
</shape>
<?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>
<?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>
<?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>
<?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>

fragment\u helloworld.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>
<?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>
<?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>
<?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>
<?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>

listitem\u helloworld.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>
<?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>
<?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>
<?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>
<?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);
}
公共类FragmentHelloWorld扩展了android.support.v4.app.Fragment{
公共碎片HelloWorld(){
}
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图v=充气机。充气(R.layout.fragment\u helloworld,容器,假);
//ListView的字符串数组
字符串[]listviewTitle=新字符串[]{
“项目1”,
“项目2”,
“项目3”
};
//ListView的图像数组
int[]listviewImage=新建int[]{
R.drawable.ic_红,
R.drawable.ic_黄色,
R.drawable.ic_绿色
};
列表列表=新的ArrayList();
对于(int i=0;i<3;i++){
HashMap hm=新的HashMap();
hm.put(“列表视图标题”,列表视图标题[i]);
hm.put(“listview_image”,Integer.toString(listviewImage[i]);
添加(hm);
}
字符串[]from={“listview\u image”,“listview\u title”};
int[]to={R.id.item\u img,R.id.item\u colortTitle};
simpledapter simpledapter=新的simpledapter(getActivity().getBaseContext(),aList,R.layout.listitem\u helloworld,from,to);
ListView list_helloworld=(ListView)v.findViewById(R.id.list_helloworld);
list_helloworld.setAdapter(simpleAdapter);
}

你可能想看看这篇文章


或者,您可以尝试为绘图表指定一个矩形,并使用
LinearLayout
,这将很简单。

为什么不在列表项中使用线性布局而不是相对布局?啊,好的。刚才注意到使用
LinearLayout
而不是
RelativeLayout
。感谢您的提示。欢迎:)