ListView 表格動態設置控件寬度

在用ListView作表格時我想你們都遇到和我同樣的問題如圖 html

解決這個問題大部都會想到動態設置控件寬度,繼承 SimpleAdapter 重寫 public View getView(int position,View convertView,ViewGroup parent)方法,代碼以下: java

  1. package com.TableTest; 
  2. import java.util.List; 
  3. import java.util.Map; 
  4. import android.content.Context; 
  5. import android.view.View; 
  6. import android.view.ViewGroup; 
  7. import android.widget.LinearLayout; 
  8. import android.widget.SimpleAdapter; 
  9. public class TableAdapter extends SimpleAdapter { 
  10.     private int screenWidth;//屏幕寬度 
  11.     public int getScreenWidth() { 
  12.         return screenWidth; 
  13.     } 
  14.     public void setScreenWidth(int screenWidth) { 
  15.         this.screenWidth = screenWidth; 
  16.     } 
  17.     public TableAdapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to) { 
  18.         super(context, data, resource, from, to); 
  19.     } 
  20.     public View getView(int position,View convertView,ViewGroup parent){ 
  21.          
  22.         int itemWidth = (screenWidth - 10) / 3
  23.         View view = super.getView(position,convertView,parent); 
  24.          
  25.         View table_item_item0 = view.findViewById(R.id.table_item_item0); 
  26.         View table_item_item1 = view.findViewById(R.id.table_item_item1); 
  27.         View table_item_item2 = view.findViewById(R.id.table_item_item2); 
  28.          
  29.         LinearLayout.LayoutParams linearParams0 = (LinearLayout.LayoutParams)table_item_item0.getLayoutParams(); 
  30.         linearParams0.width = itemWidth; 
  31.         table_item_item0.setLayoutParams(linearParams0); 
  32.          
  33.         LinearLayout.LayoutParams linearParams1 = (LinearLayout.LayoutParams)table_item_item1.getLayoutParams(); 
  34.         linearParams1.width = itemWidth; 
  35.         table_item_item1.setLayoutParams(linearParams1); 
  36.          
  37.         LinearLayout.LayoutParams linearParams2 = (LinearLayout.LayoutParams)table_item_item2.getLayoutParams(); 
  38.         linearParams2.width = itemWidth; 
  39.         table_item_item2.setLayoutParams(linearParams2); 
  40.          
  41.         return  view; 
  42.     } 
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

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="wrap_content" 
  5.     android:orientation="horizontal"> 
  6.     <TextView android:id="@+id/table_item_item0"  
  7.     android:layout_width="1000px"  
  8.     android:layout_height="wrap_content"  
  9.     android:singleLine="true"  
  10.     android:textColor="#ff133447" 
  11.     android:layout_gravity="center" 
  12.     android:gravity="center" 
  13.     android:paddingTop="2px" 
  14.     android:paddingBottom="2px" 
  15.     android:layout_weight="1" /> 
  16.     <View android:layout_width="1px"  
  17.     android:layout_height="fill_parent" 
  18.     android:background="#ff000000" /> 
  19.     <TextView android:id="@+id/table_item_item1"  
  20.     android:layout_width="1000px"  
  21.     android:layout_height="wrap_content"  
  22.     android:singleLine="true"  
  23.     android:textColor="#ff133447" 
  24.     android:layout_gravity="center" 
  25.     android:gravity="center" 
  26.     android:paddingTop="2px" 
  27.     android:paddingBottom="2px" 
  28.     android:layout_weight="1" /> 
  29.     <View android:layout_width="1px"  
  30.     android:layout_height="fill_parent"  
  31.     android:background="#ff000000" /> 
  32.     <TextView android:id="@+id/table_item_item2"  
  33.     android:layout_width="1000px"  
  34.     android:layout_height="wrap_content"  
  35.     android:singleLine="true"  
  36.     android:textColor="#ff133447" 
  37.     android:layout_gravity="center" 
  38.     android:gravity="center" 
  39.     android:paddingTop="2px" 
  40.     android:paddingBottom="2px" 
  41.     android:layout_weight="1" /> 
  42. </LinearLayout>
相關文章
相關標籤/搜索