我正在尝试在Android中重新创建以下布局:
网格不必动态增长,它将始终为5列2行。方向固定为横向。每个单元格具有完全相同的布局,只是TextViews中的数据可以更改。
我当前的方法是用TableLayouts填充GridLayout并为每个TableLayout设置适当的layout_row和layout_column。像这样:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="2"
android:columnCount="4"
android:orientation="vertical">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:background="@color/colorAccent">
这将导致以下结果:
上图中的代码:https://pastebin.com/khr9aGWf
我遇到了一些问题。
也许我的方法是完全错误的,我需要确定一次定义单元格布局并用它动态填充网格的东西吗?
任何帮助将不胜感激。
答案 0 :(得分:2)
您可以使用GridView创建这种布局
<GridView
android:id="@+id/list"
android:numColumns="5"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"/>
您的gridview项布局
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:background="@color/colorPrimary">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
GridviewAdapter.java
public class GridviewAdapter extends BaseAdapter {
Activity context;
private LayoutInflater mInflater;
TableLayout.LayoutParams params;
public GridviewAdapter(Activity context)
{
this.context=context;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
params=new TableLayout.LayoutParams(new TableLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
(int) ((getDeviceHeight(context)-getStatusBarHeight()) / 2)));
}
@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position%2==0)
{
convertView.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
}else{
convertView.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
}
convertView.setLayoutParams(params);
return convertView;
}
@Override
public int getCount() {
return 10;
}
@Override
public String getItem(int position) {
return "";
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
}
public static int getDeviceHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
Point point = new Point();
wm.getDefaultDisplay().getSize(point);
return point.y;
} else {
return wm.getDefaultDisplay().getHeight();
}
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
并在您的活动班级中添加如下代码
GridviewAdapter itemsAdapter = new GridviewAdapter(this);
GridView listView = (GridView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
您的输出看起来像this