ExpandableListView和其他组件相互整合布局
举个例子:ExpandableListActivity中包含着两大部分组件:
1. 是由搜索框和搜索按钮组合的组件
2.是标准的ExpandableListView
如下图所示:
因为,Listview和ExpandableListView都可以为其设置“表头组件”和“表尾组件”,即: addHeaderView 和addFooterView,然后做一下registerForContextMenu(listView),就可以了。具体代码如下:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
//删除N行......
//增加列表头元素
ExpandableListView listView = getExpandableListView();
//界面布局
LinearLayout myLinearLayout = new LinearLayout(this);
LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
param1.setMargins(10, 16, 10, 12);
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
param2.setMargins(0, 16, 10, 12);
myLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
myLinearLayout.setGravity(Gravity.CENTER);
//搜索框组件设置
searchEditText = new EditText(this);
searchEditText.setWidth(250);
searchEditText.setHint("搜: 名称、拼音、菜系、地址");
searchEditText.setSingleLine(true);
//搜索按钮组件设置
searchBtn = new ImageButton(this);
searchBtn.setBackgroundResource(R.drawable.btn_search);
searchBtn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View b) {
//搜索按钮的功能,以下功能和主题无关,省略......
}
});
//在布局中增加元素
myLinearLayout.addView(searchEditText,param1);
myLinearLayout.addView(searchBtn,param2);
//把组件加到表头
//关键简单的步骤,实现组合布局!
listView.addHeaderView(myLinearLayout);
registerForContextMenu(listView);
setListAdapter(mAdapter); }
|
参考网址:
阅读(2712) | 评论(0) | 转发(0) |