InsertColumn的时候第一列插入空列
list.InsertColumn(0,"A",LVCFMT_CENTER,100)
list.InsertColumn(1,"A",LVCFMT_CENTER,100)
list.InsertColumn(2,"B",LVCFMT_CENTER,100)
list.InsertColumn(3,"C",LVCFMT_CENTER,100)
list.InsertColumn(4,"D",LVCFMT_CENTER,100)
list.InsertColumn(5,"E",LVCFMT_CENTER,100)
然后把原先的列一次往后面推
最后再把第一列删除掉
list.DeleteColumn(0)
这样就可以全部居中了,而且后面Column的Index也会自动往前面补齐,不会出现Index混乱的问题
MS自己搞的,第一列式不能设置格式的,MSDN里有说明:If a column is added to a list-view control with index 0 (the leftmost column) and with LVCFMT_RIGHT or LVCFMT_CENTER specified, the text is not right-aligned or centered. The text in the index 0 column is left-aligned. Therefore if you keep inserting columns with index 0, the text in all columns are left-aligned. If you want the first column to be right-aligned or centered you can make a dummy column, then insert one or more columns with index 1 or higher and specify the alignment you require. Finally delete the dummy column.
大致意思是这样的:索引为0的列(最左边的列)如果设置了LVCFMT_RIGHT或LVCFMT_CENTER属性,上面的文字并不会右对齐或居中对
齐。索引为0 的列是左对齐。如果你要想第一列右对齐或者居中对齐,你可以这样做,先保留索引为0的列,其他的列均指定右对齐或居中对齐属性,最后删除索
引为0的列。
下面是实例代码:
m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES)
CString str[] ={_T(""), _T("AAA"), _T("BBB"), _T("CCC"), _T("DDDD"), _T("EEE")}
for(int i=0i<sizeof(str)/sizeof(str[0])i++)
{
m_list.InsertColumn(i, str[i], LVCFMT_CENTER, 100)
m_list.InsertItem(i, _T(""))
m_list.SetItemText(i, 0, _T("AAA"))
}
m_list.DeleteColumn(0)
具体代码如下(不过我的是ListActivity,你的ListView控件,二者用法没太大区别)这种方式是实现自定义显示模式的典型用法。
//在res中定义:list_layout.xml
<?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="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
/>
</LinearLayout>
//上面android:gravity="center_horizontal" 是居中的关键
//在主类中借助SimpleAdapter适配器
public class MainActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
SimpleAdapter adapter = new SimpleAdapter(this, getData(),
R.layout.list_layout, new String[] { "title" },
new int[] { R.id.title})
setListAdapter(adapter)
}
private List<Map<String, Object>>getData() {
List<Map<String, Object>>list = new ArrayList<Map<String, Object>>()
Map<String, Object>map = new HashMap<String, Object>()
map.put("title", "AAAAA")
list.add(map)
map = new HashMap<String, Object>()
map.put("title", "BBBB")
list.add(map)
map = new HashMap<String, Object>()
map.put("title", "CC")
list.add(map)
return list
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)