在用ListView作表格時我想你們都遇到和我同樣的問題如圖 html
解決這個問題大部都會想到動態設置控件寬度,繼承 SimpleAdapter 重寫 public View getView(int position,View convertView,ViewGroup parent)方法,代碼以下: java
- 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;
- }
- }
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;
}
}
效果如圖 android
工程完整代碼下載地址:http://download.csdn.net/source/3409570 this
解決上列問題還有一個更簡單的方法 就是把 表格裏的控件設置一個超長值如 android:layout_width="1000px" spa
代碼以下 .net
- <?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>