在用ListView做表格时我想大家都遇到和我一样的问题如图
解决这个问题大部都会想到动态设置控件宽度,继承 SimpleAdapter 重写 public View getView(int position,View convertView,ViewGroup parent)方法,代码如下:
- package com.TableTest;
- import java.util.List;
- import java.util.Map;
- import android.content.Context;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.LinearLayout;
- import android.widget.SimpleAdapter;
- public class TableAdapter extends SimpleAdapter {
- private int screenWidth;//屏幕宽度
- public int getScreenWidth() {
- return screenWidth;
- }
- public void setScreenWidth(int screenWidth) {
- this.screenWidth = screenWidth;
- }
- public TableAdapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to) {
- super(context, data, resource, from, to);
- }
- public View getView(int position,View convertView,ViewGroup parent){
- int itemWidth = (screenWidth - 10) / 3;
- View view = super.getView(position,convertView,parent);
- View table_item_item0 = view.findViewById(R.id.table_item_item0);
- View table_item_item1 = view.findViewById(R.id.table_item_item1);
- View table_item_item2 = view.findViewById(R.id.table_item_item2);
- LinearLayout.LayoutParams linearParams0 = (LinearLayout.LayoutParams)table_item_item0.getLayoutParams();
- linearParams0.width = itemWidth;
- table_item_item0.setLayoutParams(linearParams0);
- LinearLayout.LayoutParams linearParams1 = (LinearLayout.LayoutParams)table_item_item1.getLayoutParams();
- linearParams1.width = itemWidth;
- table_item_item1.setLayoutParams(linearParams1);
- LinearLayout.LayoutParams linearParams2 = (LinearLayout.LayoutParams)table_item_item2.getLayoutParams();
- linearParams2.width = itemWidth;
- table_item_item2.setLayoutParams(linearParams2);
- return view;
- }
- }
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
public class TableAdapter extends SimpleAdapter {
private int screenWidth;//屏幕宽度
public int getScreenWidth() {
return screenWidth;
}
public void setScreenWidth(int screenWidth) {
this.screenWidth = screenWidth;
}
public TableAdapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
public View getView(int position,View convertView,ViewGroup parent){
int itemWidth = (screenWidth - 10) / 3;
View view = super.getView(position,convertView,parent);
View table_item_item0 = view.findViewById(R.id.table_item_item0);
View table_item_item1 = view.findViewById(R.id.table_item_item1);
View table_item_item2 = view.findViewById(R.id.table_item_item2);
LinearLayout.LayoutParams linearParams0 = (LinearLayout.LayoutParams)table_item_item0.getLayoutParams();
linearParams0.width = itemWidth;
table_item_item0.setLayoutParams(linearParams0);
LinearLayout.LayoutParams linearParams1 = (LinearLayout.LayoutParams)table_item_item1.getLayoutParams();
linearParams1.width = itemWidth;
table_item_item1.setLayoutParams(linearParams1);
LinearLayout.LayoutParams linearParams2 = (LinearLayout.LayoutParams)table_item_item2.getLayoutParams();
linearParams2.width = itemWidth;
table_item_item2.setLayoutParams(linearParams2);
return view;
}
}
效果如图
工程完整代码下载地址:http://download.csdn.net/source/3409570
解决上列问题还有一个更简单的方法 就是把 表格里的控件设置一个超长值如 android:layout_width="1000px"
代码如下
- <?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="wrap_content"
- android:orientation="horizontal">
- <TextView android:id="@+id/table_item_item0"
- android:layout_width="1000px"
- android:layout_height="wrap_content"
- android:singleLine="true"
- android:textColor="#ff133447"
- android:layout_gravity="center"
- android:gravity="center"
- android:paddingTop="2px"
- android:paddingBottom="2px"
- android:layout_weight="1" />
- <View android:layout_width="1px"
- android:layout_height="fill_parent"
- android:background="#ff000000" />
- <TextView android:id="@+id/table_item_item1"
- android:layout_width="1000px"
- android:layout_height="wrap_content"
- android:singleLine="true"
- android:textColor="#ff133447"
- android:layout_gravity="center"
- android:gravity="center"
- android:paddingTop="2px"
- android:paddingBottom="2px"
- android:layout_weight="1" />
- <View android:layout_width="1px"
- android:layout_height="fill_parent"
- android:background="#ff000000" />
- <TextView android:id="@+id/table_item_item2"
- android:layout_width="1000px"
- android:layout_height="wrap_content"
- android:singleLine="true"
- android:textColor="#ff133447"
- android:layout_gravity="center"
- android:gravity="center"
- android:paddingTop="2px"
- android:paddingBottom="2px"
- android:layout_weight="1" />
- </LinearLayout>