编辑listView,margin top

时间:2012-01-04 16:16:28

标签: java android listview

我有一个类似于此的列表视图

listview

那就是它的代码..

package com.a.c;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class calender extends ListActivity {
   /** Called when the activity is first created. */
   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, calendr));
    getListView().setTextFilterEnabled(true);

}

static final String[] calendr = new String[]{
    "MONT Blanc",
    "Gucci",
    "Parker",
    "Sailor",
    "Porsche Design",
    "Rotring",
    "Sheaffer",
    "Waterman"
};}

我想修改它以添加此布局,并将listview向下移动一点

图像看起来像一个相框,这就是我想移动列表视图的原因..

.XML文件的布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativeLayout1"
 android:layout_width="600px"
 android:layout_height="1024px"
 xmlns:android="http://schemas.android.com/apk/res/android">


<ImageView android:id="@+id/layout" 
            android:src="@drawable/layout" 
            android:layout_width="wrap_content" 
            android:layout_height="fill_parent"
           android:layout_alignParentBottom="true" 
           android:layout_alignParentLeft="true" 
           android:layout_alignParentRight="true" 
          android:layout_alignParentTop="true"></ImageView>

       </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您好您可以使用此代码并实现目标:

   main.xml
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/layout"
        android:src="@drawable/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ImageView>
    <ListView
        android:layout_width="match_parent"
        android:id="@+id/listViewScore"
        android:cacheColorHint="#00000000"
        android:layout_height="match_parent"
        android:layout_weight="1.00"
        android:divider="#C0C0C0"
        android:layout_below="@+id/layout"
        android:dividerHeight="2dip" />

</RelativeLayout>


  listviewtext.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">
    <TextView
        android:text="Name"
        android:id="@+id/textViewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"></TextView>


</LinearLayout>


    public class TestProjeectActivity extends Activity {

        private ListView listViewScore = null;
        private ListViewAdapter listViewAdapter = null;
        private String[] usernameArr = null;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            listViewScore=(ListView)findViewById(R.id.listViewScore);

            usernameArr = new String[]{"Alan","Bob","Carl","Doaniel","Evan","Fred","Gram","Ian","Jordan"};

            listViewAdapter = new ListViewAdapter();
            listViewScore.setAdapter(listViewAdapter);
        }

        class ListViewAdapter extends BaseAdapter{

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                if(usernameArr==null){
                    return 0;
                }

                return usernameArr.length;
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return usernameArr[position];
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public View getView(int position, View view, ViewGroup parent) {
                // TODO Auto-generated method stub
                View rowView=view;

                if(rowView==null){
                    LayoutInflater layoutinflate =LayoutInflater.from(TestProjeectActivity.this);
                    rowView=layoutinflate.inflate(R.layout.listviewtext, parent, false);
                }

                TextView textViewName=(TextView)rowView.findViewById(R.id.textViewName);

                textViewName.setText(usernameArr[position]);

                return rowView;
            }

        }
    }