如何将按钮文本放在中心

时间:2011-10-13 13:33:30

标签: android

在我的按钮数组中,我想将每个按钮的文本放在中心。我使用了Gravity,但它不起作用。我使用的代码是。请任何人帮我解决这个问题。

    LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
    LinearLayout rowLayout = null;

    LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);

    //Create Button
    for (int i = 0; i<6; i++)
    {
        rowLayout = new LinearLayout(this);
        rowLayout.setWeightSum(7);
        layoutVertical.addView(rowLayout, param);   

        for(int j=0; j<7; j++)
        {
            m_pBtnDay[i][j] = new Button(this);             

            m_pBtnDay[i][j].setTextSize(12);
            m_pBtnDay[i][j].setGravity(Gravity.CENTER);

            rowLayout.addView(m_pBtnDay[i][j], param); 

            m_pBtnDay[i][j].setOnLongClickListener(this);                           
            m_pBtnDay[i][j].setOnClickListener(this);

            m_pBtnDay[i][j].setOnTouchListener(this);              

            //save button position
            m_pBtnDay[i][j].setTag(new CalendarForm(i , j));
        }
    }

1 个答案:

答案 0 :(得分:0)

试试这个:m_pBtnDay[i][j].getPaint().setTextAlign(Paint.Align.CENTER);