如何水平居中ListView页脚

时间:2011-08-03 03:23:33

标签: android

我已经在我的ListView中添加了一个页脚,如下所示:

...{
  listView.addFooterView(getButtonFooter(context));
...}

    private View getButtonFooter(Context context) {
        Button button = new Button(context);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        button.setBackgroundResource(R.drawable.add_reminder_btn_selector);
        button.setId(ADD_REMINDER);
        button.setOnClickListener(this);
        return button;
    }

我试图弄清楚我现在可以如何水平居中按钮。

编辑:哦,也许是这样的? button.setGravity(Gravity.CENTER_HORIZONTAL);

编辑:lol,nope。这似乎不起作用。猜猜只是按下按钮内的文字......

1 个答案:

答案 0 :(得分:3)

LinearLayout layout = new LinearLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        layout.setGravity(Gravity.CENTER_HORIZONTAL);
        Button button = new Button(this);
        button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        button.setBackgroundResource(R.drawable.add_reminder_btn_selector);
        button.setId(ADD_REMINDER);
        button.setOnClickListener(this);
        layout.addView(button);
        return layout;