我按语法添加了列表视图的Button页脚视图。现在我想设置按钮的参数,并将按钮的重心设置为垂直中心。我按以下方式做了,但不起作用。如何实现?
btnMoreUpcomingExits = new Button(this);
btnMoreUpcomingExits.setText("More Upcoming Exits");
btnMoreUpcomingExits.setBackgroundResource(R.layout.moreupebtn_widget);
btnMoreUpcomingExits.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.WRAP_CONTENT, ListView.LayoutParams.WRAP_CONTENT));
btnMoreUpcomingExits.setTextColor(Color.WHITE);
btnMoreUpcomingExits.setTypeface(null, Typeface.BOLD);
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
btnMoreUpcomingExits.setOnClickListener(btnMoreUpcomingExitsListener);
getListView().addFooterView(btnMoreUpcomingExits);
setListAdapter(new UpcomingExitsResultsListViewAdapter(this));
您当前拥有
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
我想这是一个疏忽,你是说CENTER_VERTICAL?这将垂直对齐按钮内的文本,它应该已经这样做了,所以你的问题看起来很困惑。你是不是在问如何将按钮本身垂直对齐?如果是这样,则需要在父按钮上设置重力。
至于参数,您已经在设置布局参数了。你还想做什么?你的问题应该更加具体,这样更容易确定你想做什么。
试试这个:
LinearLayout layout=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
btnMoreUpcomingExits = new Button(this);
btnMoreUpcomingExits.setText("More Upcoming Exits");
btnMoreUpcomingExits.setBackgroundResource(R.layout.moreupebtn_widget);
btnMoreUpcomingExits.setLayoutParams(params);
btnMoreUpcomingExits.setTextColor(Color.WHITE);
btnMoreUpcomingExits.setTypeface(null, Typeface.BOLD);
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
layout.addView(btnMoreUpcomingExits);
btnMoreUpcomingExits.setOnClickListener(btnMoreUpcomingExitsListener);
getListView().addFooterView(btnMoreUpcomingExits);
setListAdapter(new UpcomingExitsResultsListViewAdapter(this));
检查一下,这可能会对你有所帮助。
检查此链接
您可以使用WindowManager.LayoutParams
在listview
中设置button
的属性。