TableLayout Android java设置为完全正确?

时间:2012-02-29 04:41:21

标签: java android tablelayout

public void responsePost(Editable editable)
    {
         TableLayout chatbox = (TableLayout)findViewById(R.id.chatbox);
         TableRow tr1 = new TableRow(this);
         tr1.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
         TextView textview = new TextView(this);
         textview.setText(editable);
       //  textview.getTextColors(R.color.)
         textview.setTextColor(Color.YELLOW);

         tr1.addView(textview);
         chatbox.addView(tr1, new TableLayout.LayoutParams(
         LayoutParams.FILL_PARENT,
         LayoutParams.WRAP_CONTENT));

           textview.setGravity(Gravity.RIGHT);    
    }

我怎样才能让它走向右边?而不仅仅是一点点?我使用了setGravity(Gravity.RIGHT)但是没有用。

There is a picture of what it is doing.

1 个答案:

答案 0 :(得分:0)

我需要更多详细信息,我无法复制您的布局,您在主要布局中有什么,以及您在onCreate中为活动提供了哪些代码?

您只需将列设置为可伸缩:

chatbox.setColumnStretchable(1, true);

您需要一个额外的行,请将其留空

TextView col1 = new TextView(this);
col1.setText("");
tr1.addView(col1);
例如,完整的代码可以是:

public class So extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button); 

        button.setOnClickListener(new View.OnClickListener() {
            // anonymous inner class
            public void onClick(View view) {
                EditText editText1 = (EditText) findViewById(R.id.edit_text);
                Editable editable = editText1.getText();
                responsePost(editable);
                editText1.setText("");
            }
        });
    }

    public void responsePost(Editable editable) {
        TableLayout chatbox = (TableLayout) findViewById(R.id.chatbox);
        TableRow tr1 = new TableRow(this);
        // add these lines here
        TextView col1 = new TextView(this);
        col1.setText("");
        tr1.addView(col1);
        TextView textview = new TextView(this);
        textview.setGravity(Gravity.RIGHT);
        textview.setText(editable);
        textview.setTextColor(Color.YELLOW);
        tr1.addView(textview);
        // and this line
        chatbox.setColumnStretchable(1, true);
        chatbox.addView(tr1, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    }
}