Android 如何在gridview行和列周围创建边框
我已经使用了一个gridview和自定义适配器以及行的文本视图。代码如下: ScheduleListActivity.javaAndroid 如何在gridview行和列周围创建边框,android,gridview,Android,Gridview,我已经使用了一个gridview和自定义适配器以及行的文本视图。代码如下: ScheduleListActivity.java public class ScheduleListActivity extends android.app.Activity { GridView gridView; ScheduleModel aSchedulObj; ArrayList<ScheduleModel> schedul_ar
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扩展了android.app.Activity
{
GridView;
ScheduleModel ASScheduleObj;
ArrayList schedul_arr;
GetCityFromAsync-cityAsync;
适配器时间表我的适配器;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u view\u schedule);
schedul_arr=新数组列表();
gridView=(gridView)findViewById(R.id.lst\u all\u时间表);
cityAsync=新建GetCityFromAsync();
citysync.execute();
}
私有类GetCityFromAsync扩展异步任务
{
语境;
@凌驾
受保护的void onPreExecute()
{super.onPreExecute();}
@凌驾
受保护的字符串doInBackground(无效…arg0)
{返回null;}
@凌驾
受保护的void onPostExecute(字符串结果)
{
super.onPostExecute(结果);
试一试{
AsScheduleObj=新的时间表模型(“Shaon”、“2015年9月23日”、“12:58”、“1:00”、“92.800”、“101.02”、“Mirpur、kazipara”);
日程安排附件(aSchedulObj);
AsScheduleObj=新的调度模型(“aaaa”、“bbb”、“12:58”、“1:00”、“92.800”、“101.02”、“rokeya sharani、kazipara”);
日程安排附件(aSchedulObj);
AsScheduleObj=新的ScheduleModel(“xxx”、“yyy”、“1:58”、“2:00”、“55.00”、“60.55”、“panthapath,kalabagan”);
日程安排附件(aSchedulObj);
myAdapter=新适配器时间表列表(getApplicationContext(),schedul\u arr);
setAdapter(myAdapter);
}
捕获(例外e){
e、 printStackTrace();}
}
}
}
活动\u视图\u ScheduleListActivity的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;
}
}
公共类适配器ScheduleList扩展BaseAdapter{
语境(adapter Context);;
ArrayList sch_arr;
公共适配器时间表列表(上下文,
ArrayList时间表){
_adapterContext=上下文;
this.schu_arr=计划;
}
public void updateList(){notifyDataSetChanged();}
@凌驾
public int getCount(){return schu arr.size();}
@凌驾
公共调度模型getItem(int位置){return schu arr.get(position);}
@凌驾
public long getItemId(int position){return long.valueOf(sch_arr.get(position.getScheduleID());}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气器=(LayoutInflater)\适配器上下文
.getSystemService(上下文布局\充气机\服务);
视图行视图=充气机。充气(R.layout.listitem_明细表,父项,false);
最终ScheduleModel _aSchdl=(ScheduleModel)getItem(位置);
TextView txtVwName=(TextView)rowView.findViewById(R.id.grid\u item\u name);
txtVwName.setText(_aSchdl.getClientName());
TextView txtVwDate=(TextView)rowView.findViewById(R.id.grid\u item\u date);
txtVwDate.setText(String.valueOf(_aSchdl.getScheduleDate());
TextView txtVwStartTime=(TextView)rowView.findViewById(R.id.grid\u item\u label\u starttime);
txtVwStartTime.setText(String.valueOf(_aSchdl.getScheduleStartTime());
TextView txtVwEndTIme=(TextView)rowView.findViewById(R.id.grid\u item\u endTime);
setText(String.valueOf(_aSchdl.getScheduleEndTime());
TextView txtVwAddress=(TextView)rowView.findViewById(R.id.grid\u item\u address);
txtVwAddress.setText(String.valueOf(_aSchdl.getScheduleAddress());
返回行视图;
}
}
行项目布局-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>
<?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>
现在我需要的是行和列的边框。这怎么可能?由于我尝试了几种解决方案,但都失败了,所以最后不得不使用下划线边框视图
请帮助我:-)给gridview留一个空白,这样它就比它的容器小 然后,“边框”将是容器本身,显示其背景色/图像 这就是如何在gridview周围创建边框的方法
希望有帮助。在您的drawable文件夹中创建下面的drawable resource border.xml,并将其设置为您的
listitem\u schedule.xml的背景
border.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>
<?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>
在您的列表项_schedule.xml
一条直线布局中,设置如下背景
android:background=“@drawable/border”
我推荐这篇文章