android - 如何水平对齐一些以编程方式添加的 View ?

标签 android android-layout alignment

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

image

EditText 中输入文本后,当我按下 Add+ Button 时,TextView将添加按钮,如下图所示:

image

我想在 TextView 的右侧显示 Button。我应该怎么做? 另一个问题,当用户点击按钮时,我应该如何删除相应的 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;
    }
  }

最佳答案

TextViewsButtons 是堆叠在一起的,因为您可能使用方向为垂直LinearLayout。您可以将 TextView + Button 包装到 LinearLayout 中,然后将此 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,请使用 LinearLayout 来包裹您的 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;
    }

关于android - 如何水平对齐一些以编程方式添加的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10796075/

相关文章:

c - C中的内存对齐

检查并对齐缓冲区

找不到 Android sdkmanager 工具

android - 从ndk中的不同线程调用java方法

android - 如何在 Android 中维护多个页脚 View ?

android - 防止 Android 屏幕在 Activity 运行时变暗

android - FragmentTransaction.replace() 不工作

android - Actionbarsherlock 底栏

android - 制作自定义菜单,项目数量未知

css - DIV 中的中心图像(原始图像可以比 DIV 大)