为什么'\ n'字符拉伸我的TextView?

时间:2012-03-20 19:31:41

标签: java android textview

我有10个垂直TextView,其中包含两行数字,由一行分隔。

seatStats2[i].setText(0 + "\n" + 0);

首次显示时,这些textViews显示正常: enter image description here

然而,如果我单击或长按屏幕上的十个按钮中的任何一个,则textViews伸展到包括第一行上的\ n字符(我测试了不同的数字组合以确认这一点)。

enter image description here

单击按钮的方法无论如何都没有链接到TextViews(LongClicking但都会影响它)。无论如何,我会为两个听众提供代码。 textView2是包含有问题的textViews的数组。我希望保持textViews伸展的能力,如果我放入更多的数字,但我想删除点击之间不必要的拉伸。

//Creates longClickListener, used for players to sit in or out.
    longClickListener = new View.OnLongClickListener() 
    {   
        @Override
        public boolean onLongClick(View v) 
        {
            if(moveButtonMode)
                return false;

            for(int i=0; i<10; i++)
            {
                //Each seat[i] will correspond with each imageButtoni+1
                if(v.getId() == (getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud"))) 
                {
                    //If the seat is empty fill it, place a player in the seat and change the buttons colour to grey and setup stat panel
                    if(table.seats[i].getState().equals("empty"))
                    {
                        seatButton[i].setImageResource(R.drawable.black_seat);
                        table.seats[i].sit(new Player());
                        table.incrementOccupiedSeatCount();
                        seatStats[i].setVisibility(View.VISIBLE);
                        seatStats2[i].setVisibility(View.VISIBLE);
                        seatStats[i].setText(table.seats[i].getPlayer().getVpip() + "/" + table.seats[i].getPlayer().getPfr() + "/" + table.seats[i].getPlayer().getHands());
                        seatStats2[i].setText(table.seats[i].getPlayer().getSteal() + "\n" + table.seats[i].getPlayer().getThreeBet());
                    }
                    //If the seat is full, empty it
                    else
                    {
                        seatButton[i].setImageResource(R.drawable.dotted_box);
                        seatStats[i].setText("");
                        table.seats[i].sitOut();
                        table.decrementOccupiedSeatCount();
                        seatStats[i].setVisibility(View.INVISIBLE);
                        seatStats2[i].setVisibility(View.INVISIBLE);
                    }
                }
            }
            return true;
        }
    };



public void playerClickhandler(View v)
{
    if(!moveButtonMode)
    {
        for(int i = 0; i<10; i++)
        {
            if(v.getId() == getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud"))
            {
                if(table.seats[i].getState() == "active")
                {
                    table.seats[i].setState("called");
                    seatButton[i].setImageResource(R.drawable.green_seat);
                }
                else if(table.seats[i].getState() == "called")
                {
                    table.seats[i].setState("raised");
                    seatButton[i].setImageResource(R.drawable.orange_seat);
                }
                else if(table.seats[i].getState() == "raised")
                {
                    table.seats[i].setState("active");
                    seatButton[i].setImageResource(R.drawable.grey_seat);
                }
            }
        }
    }
    else
    {
        for(int i = 0; i<10; i++)
        {
            if(v.getId() == getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud"))
            {
                dealerBtnImage[table.getButtonSeat()].setVisibility(View.INVISIBLE);
                table.passButton(i);
                dealerBtnImage[table.getButtonSeat()].setVisibility(View.VISIBLE);
                moveButtonMode = false;
                nextHandButton.setEnabled(true);
            }
        }
    }
}

XML TextView示例

<TextView android:id="@+id/textView19" android:visibility="invisible" android:background="#000000" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_toRightOf="@+id/imageButton9" android:layout_alignParentTop="true"></TextView>
enter code here

1 个答案:

答案 0 :(得分:1)

我在您提供的代码中看不到任何错误!

因此,我最好的选择是在以下其中一个获取者中增加空白

table.seats[i].getPlayer().getSteal()

table.seats[i].getPlayer().getThreeBet()  // this would be more likely

从你的代码开始,我认为它们代表一个字符串,也许你可以在它们上调用 trim(),看它是否有帮助。

但正如已经说过的,这只是一个建议。

希望有这种帮助,玩得开心!