1

我设计了如下图所示的布局:

图片

在 中输入文本后EditText,当我按Add+ ButtonTextViewButton将添加如下图所示:

图片

我想Button在右侧显示TextView. 我该怎么做?View另一个问题,当用户单击按钮时,我应该如何删除对应的?编码:

 public class ExampleActivity extends Activity {
    private LinearLayout mLayout;
    private EditText mEditText;
    private Button mButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLayout = (LinearLayout) findViewById(R.id.linearLayout);
        mEditText = (EditText) findViewById(R.id.editText);
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mLayout.addView(createNewTextView(mEditText.getText()
                        .toString()));
                mLayout.addView(createNewButton());
            }
        });

    }

    private TextView createNewTextView(String text) {
        final LayoutParams lparams = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        final TextView textView = new TextView(this);
        textView.setLayoutParams(lparams);
        textView.setText("New text: " + text);
        return textView;
    }

    private Button createNewButton() {
        final LayoutParams lparams = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        final Button button = new Button(this);
        button.setLayoutParams(lparams);
        button.setText(" - ");
        return button;
    }
  }
4

2 回答 2

1

TextViewsButtons是堆叠的,因为您可能将 aLinearLayout与方向一起使用vertical。您可以将您的TextView+包装Button成 aLinearLayout然后将其添加LinearLayout到您自己的布局中,或者您可以使用TableLayout下面的类似(我添加了一些 id,因此您可以删除所需的行):

public class SomeActivity extends Activity {

    private EditText mInput;
        private TableLayout mTable;
        private static int sCount = 0;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button addButton = (Button) findViewById(R.id.add);
            mInput = (EditText) findViewById(R.id.editText1);
            mTable = (TableLayout) findViewById(R.id.table1);
            addButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    mTable.addView(addRow(mInput.getText().toString()));
                }
            });
        }

        private TableRow addRow(String s) {
            TableRow tr = new TableRow(this);
            tr.setId(1000 + sCount);
            tr.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.FILL_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT));
            TableRow.LayoutParams tlparams = new TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            TextView textView = new TextView(this);
            textView.setLayoutParams(tlparams);
            textView.setText("New text: " + s);
            tr.addView(textView);
            TableRow.LayoutParams blparams = new TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            final Button button = new Button(this);
            button.setLayoutParams(blparams);
            button.setText(" - ");
            button.setId(2000 + sCount);
            button.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {               
                    mTable.removeView(findViewById(v.getId() - 1000));
                }           
            });
            tr.addView(button);
            sCount++;
            return tr;
    }

}

main布局文件在哪里:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/add"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TableLayout
            android:id="@+id/table1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TableLayout>
    </LinearLayout>

</ScrollView>

如果由于某种原因,您不喜欢TableLayout使用 aLinearLayout来包装您TextViewButton使用上面的布局文件(当然删除TableLayout):

//...
ll = (LinearLayout) findViewById(R.id.parent);
        addButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //where ll is the LinearLayout with the id parent
                ll.addView(addRow(mInput.getText().toString()));
            }
        });
    }

    private LinearLayout addRow(String s) {
        LinearLayout tr = new LinearLayout(this);
        tr.setId(1000 + sCount);
        tr.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        LinearLayout.LayoutParams tlparams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        TextView textView = new TextView(this);
        textView.setLayoutParams(tlparams);
        textView.setText("New text: " + s);
        tr.addView(textView);
        LinearLayout.LayoutParams blparams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        final Button button = new Button(this);
        button.setLayoutParams(blparams);
        button.setText(" - ");
        button.setId(2000 + sCount);
        button.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {               
                ll.removeView(findViewById(v.getId() - 1000));
            }           
        });
        tr.addView(button);
        sCount++;
        return tr;
    }
于 2012-05-30T07:33:32.020 回答
1

LinearLayout 具有属性 Orientation 以垂直/水平对齐控件,因此只需设置相同的 Orientation

http://developer.android.com/reference/android/widget/LinearLayout.html

 mLayout = (LinearLayout) findViewById(R.id.linearLayout);
 mLayout.setOrientation(LinearLayout.HORIZONTAL);

更新

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout   
        xmlns:android="schemas.android.com/apk/res/android";   
        android:layout_width="fill_parent"   
        android:layout_height="fill_parent" > 

<LinearLayout   
        android:id="@+id/linearLayout" 
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content" /> 

<EditText   
        android:id="@+id/editText" 
        android:layout_width="293dp"   
        android:layout_height="wrap_content" > 
        <requestFocus /> </EditText> 

 <Button android:id="@+id/button" 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Add+" />

 </LinearLayout>
于 2012-05-29T09:06:51.017 回答