如何围绕gridview行和列创建边框

时间:2015-09-23 07:27:29

标签: android gridview

我使用了一个带有自定义适配器的gridview和用于行的textViews。以下是代码:

ScheduleListActivity.java

    public class ScheduleListActivity extends android.app.Activity 
    {
        GridView gridView;
        ScheduleModel  aSchedulObj;
        ArrayList<ScheduleModel> schedul_arr;
        GetCityFromAsync cityAsync;
        AdapterScheduleList myAdapter;

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view_schedule);
           schedul_arr = new ArrayList<ScheduleModel>();
           gridView = (GridView) findViewById(R.id.lst_all_schedule);

            cityAsync =new GetCityFromAsync();
            cityAsync.execute();
        }

        private class GetCityFromAsync extends AsyncTask<Void, Void, String>
        {
            Context context;

            @Override
            protected void onPreExecute() 
                { super.onPreExecute();     }

            @Override
            protected String doInBackground(Void... arg0) 
                { return null; }

            @Override
            protected void onPostExecute(String result) 
              {
                super.onPostExecute(result);

                    try {
                        aSchedulObj = new ScheduleModel("Shaon", "23-Sep-2015", "12:58", "1:00", "92.800", "101.02", "Mirpur, kazipara");
                        schedul_arr.add(aSchedulObj);

                        aSchedulObj = new ScheduleModel("aaaa", "bbb", "12:58", "1:00", "92.800", "101.02", "rokeya sharani, kazipara");
                        schedul_arr.add(aSchedulObj);

                        aSchedulObj = new ScheduleModel("xxx", "yyy", "1:58", "2:00", "55.00", "60.55", "panthapath, kalabagan");
                        schedul_arr.add(aSchedulObj);

                            myAdapter = new AdapterScheduleList(getApplicationContext(), schedul_arr);
                            gridView.setAdapter(myAdapter);
                        }
                     catch (Exception e) {
                        e.printStackTrace();    }
            }
        }
    }
ScheduleListActivity的

activity_view_schedule.xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFF99"
    android:orientation="vertical"
    android:padding="2dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#7D7D7D"
        android:orientation="horizontal"
        android:weightSum="6" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1.45"
            android:background="#7D7D7D"
            android:padding="3dp"
            android:paddingLeft="10dp"
            android:text="Client Name"
            android:textColor="#FF9900"
            android:textSize="14sp"
            android:typeface="serif" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1.35"
            android:background="#7D7D7D"
            android:padding="3dp"
            android:paddingLeft="10dp"
            android:text="Schedule Date"
            android:textColor="#FF9900"
            android:textSize="14sp"
            android:typeface="serif" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="0.9"
            android:background="#7D7D7D"
            android:padding="3dp"
            android:paddingLeft="10dp"
            android:text="Start Time"
            android:textColor="#FF9900"
            android:textSize="14sp"
            android:typeface="serif" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="0.9"
            android:background="#7D7D7D"
            android:padding="3dp"
            android:paddingLeft="10dp"
            android:text="End Time"
            android:textColor="#FF9900"
            android:textSize="14sp"
            android:typeface="serif" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1.4"
            android:background="#7D7D7D"
            android:padding="3dp"
            android:paddingLeft="10dp"
            android:text="Location Address"
            android:textColor="#FF9900"
            android:textSize="14sp"
            android:typeface="serif" />
    </LinearLayout>

    <GridView
        android:id="@+id/lst_all_schedule"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="100dp"
        android:listSelector="@android:color/transparent"
        android:numColumns="1"
        android:stretchMode="columnWidth" >
    </GridView>

</LinearLayout>

gridview适配器

    public class AdapterScheduleList extends BaseAdapter {

        Context _adapterContext;
        ArrayList<ScheduleModel> sch_arr;

        public AdapterScheduleList(Context context,
                ArrayList<ScheduleModel> scheduls) {
            _adapterContext = context;
            this.sch_arr = scheduls;
        }

        public void updateList() {  notifyDataSetChanged(); }

        @Override
        public int getCount() {     return sch_arr.size();  }

        @Override
        public ScheduleModel getItem(int position) { return sch_arr.get(position);  }

        @Override
        public long getItemId(int position) {return Long.valueOf(sch_arr.get(position).getScheduleID());    }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) _adapterContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View rowView = inflater.inflate(R.layout.listitem_schedule, parent,false);

            final ScheduleModel _aSchdl = (ScheduleModel) getItem(position);

            TextView txtVwName = (TextView) rowView.findViewById(R.id.grid_item_name);
            txtVwName.setText(_aSchdl.getClientName());

            TextView txtVwDate = (TextView) rowView.findViewById(R.id.grid_item_date);
            txtVwDate.setText(String.valueOf(_aSchdl.getScheduleDate()));

            TextView txtVwStartTime = (TextView) rowView.findViewById(R.id.grid_item_label_starttime);
            txtVwStartTime.setText(String.valueOf(_aSchdl.getScheduleStartTime()));

            TextView txtVwEndTIme = (TextView) rowView.findViewById(R.id.grid_item_endTime);
            txtVwEndTIme.setText(String.valueOf(_aSchdl.getScheduleEndTime()));

            TextView txtVwAddress = (TextView) rowView.findViewById(R.id.grid_item_address);
            txtVwAddress.setText(String.valueOf(_aSchdl.getScheduleAddress()));

            return rowView;
        }

    }

行项目布局 - listitem_schedule.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#1975A3"
    android:orientation="vertical"
    android:padding="5dp" >

    <LinearLayout
        android:id="@+id/cityListFromItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/grid_item_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1.45"
            android:padding="3dp"
            android:text="xxx"
            android:textColor="#FFFFFF" >
        </TextView>

        <TextView
            android:id="@+id/grid_item_date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1.35"
            android:padding="3dp"
            android:text="xxx"
            android:textColor="#FFFFFF" >
        </TextView>

        <TextView
            android:id="@+id/grid_item_label_starttime"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="0.9"
            android:padding="3dp"
            android:text="xxx"
            android:textColor="#FFFFFF" >
        </TextView>

        <TextView
            android:id="@+id/grid_item_endTime"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_weight="0.9"
            android:padding="3dp"
            android:text="xxx"
            android:textColor="#FFFFFF" >
        </TextView>

        <TextView
            android:id="@+id/grid_item_address"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_weight="1.4"
            android:padding="3dp"
            android:text="xxx"
            android:textColor="#FFFFFF" >
        </TextView>
    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:layout_marginTop="10dp"
        android:background="#FFFFCC" />

</LinearLayout>

现在我需要的是行和列的边框。怎么可能?由于我尝试了几种解决方案而失败了,所以最后不得不使用视图作为下划线边框。

请帮帮我: - )

2 个答案:

答案 0 :(得分:0)

为您的gridview提供一个边距,使其小于其容器。

然后“边框”将成为容器本身,显示其背景颜色/图像。

这就是你如何围绕gridview制作边框

希望它有所帮助。

答案 1 :(得分:0)

在drawable文件夹中创建以下可绘制资源border.xml,并将其设置为listitem_schedule.xml

的背景

<强> border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <!-- use this for transparent -->
   <!-- <solid android:color="#00000000" /> -->
   <!-- use this for a background colour -->
   <solid android:color="#FFF" />
   <stroke android:width="2dip" android:color="#FF0000" />
</shape>

并在您的listitem_schedule.xml aprent LinearLayout设置背景中,如下所示 android:background="@drawable/border"