ListView 表格动态设置控件宽度

在用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> 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" 

代码如下

 

工程完整代码下载地址:http://download.csdn.net/source/3409613

 

你可能感兴趣的:(android,listview,android,layout,table,encoding,string)