在Android中,可以通过自定义Adapter或者使用ListView、RecyclerView等控件来实现表格(table)的展示。以下是一种常见的自定义方法:
public class CustomAdapter extends BaseAdapter {
private Context context;
private List<String> dataList;
public CustomAdapter(Context context, List<String> dataList) {
this.context = context;
this.dataList = dataList;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.table_row, parent, false);
}
TextView cell = convertView.findViewById(R.id.cell);
cell.setText(dataList.get(position));
return convertView;
}
}
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:background="@android:color/darker_gray"
android:textColor="@android:color/white"
android:gravity="center_horizontal"/>
public class MainActivity extends AppCompatActivity {
private List<String> dataList = Arrays.asList("Cell1", "Cell2", "Cell3", "Cell4");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.listView);
CustomAdapter adapter = new CustomAdapter(this, dataList);
listView.setAdapter(adapter);
}
}
通过以上方法,可以实现在Android应用中自定义表格的展示。可以根据实际需求定制表格的样式和内容。