Curved border for list view

I am newbie to android and java, i need to create a simple application in android. I have a Activity page in this activity. Here is my activity page

package com.tkcmu.dev;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class tkcmu extends Activity {
    private ListView lv1;

    @Override
        public void onCreate(Bundle icicle)
        {
        super.onCreate(icicle);
        setContentView(R.layout.main);


        ArrayList<String> listItems = new ArrayList<String>();

        try {
            URL recentUrl = new URL(
                    "To some link");
            URLConnection tc = recentUrl.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

            String line;
            while ((line = in.readLine()) != null) {
                JSONArray ja = new JSONArray(line);

                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    listItems.add(jo.getString("content"));
                }
            }

    }catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch (IOException e) {
                e.printStackTrace();

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        lv1=(ListView)findViewById(R.id.ListView01);
        // By using setAdpater method in listview we an add string array in list.
        lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , listItems));
        }
}

and my main.xml is like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/ListView01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10sp"
        android:layout_marginRight="10sp"
        android:layout_marginTop="10sp"
        android:layout_weight="1"
        android:background="@drawable/customshape"
        android:cacheColorHint="#FFFFFF"
        android:clickable="true"
        android:clipToPadding="true"
        android:dividerHeight="1px"
        android:drawSelectorOnTop="false"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:footerDividersEnabled="true"
        android:headerDividersEnabled="true"
        android:longClickable="true" />
</LinearLayout>

and also i have an customshape.xml in drawable which is

 <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp" />
</shape>

I got the result with the entire custom list section get curved but i want to get each row with a curved background. I have posted my entire code here(exclude AndroidManifest.xml ) so this will be appreciated if anybody tell me exactly what changes i need to in this code. Any body have an idea about how to implement it.


add

 android:background="@drawable/customshape" 

to the root view of the listview row.xml file rather than applying it to the listview


I never done this for rounded corners, but I would try to provide a custom adapter for the list. A custom adapter would allow you to specify which view to use for each item. In your case, you could try to provide a view with a rounded corner borders (defined with a shape XML).

To do this, you would replace the line lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , listItems));

with a BaseAdapter that implements the getView() method.

There are literally hundreds of tutorials on how to do this, but this one seems to be more detailed.


I would recommend you to use a custom adapter where you can set up the shape as background. This is easily achievable there. But since you are a newbie, it will be a little tough. So there is something which I won't recommend but might be a solution: Press control + B on the android.R.layout.simple_list_item_1. This is the original list view XML which is written by the Android team. So you have to override this and set your background shape here (the shape you have written, set it as the background).

链接地址: http://www.djcxy.com/p/5316.html

上一篇: 自定义状态栏通知Android中的空白区域

下一篇: 列表视图的曲线边框