如何水平对齐一些编程方式添加的看法? [英] How to horizontally align some programmatically added views?

查看:116
本文介绍了如何水平对齐一些编程方式添加的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设计了一个布局,如下图所示:

在进入了的EditText ,当我preSS中的添加+ 按钮的TextView 按钮将被添加如下所示的图像:

我要显示的按钮右侧的的TextView 的。我应该怎么办呢? 还有一个问题,我应该怎么去除与查看当用户点击一个按钮?在code:

 公共类为ExampleActivity延伸活动{
    私人的LinearLayout mLayout;
    私人的EditText mEditText;
    私人按钮mButton;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        mLayout =(的LinearLayout)findViewById(R.id.linearLayout);
        mEditText =(EditText上)findViewById(R.id.editText);
        mButton =(按钮)findViewById(R.id.button);
        mButton.setOnClickListener(新OnClickListener(){

            公共无效的onClick(视图v){
                mLayout.addView(createNewTextView(mEditText.getText()
                        的ToString()));
                mLayout.addView(createNewButton());
            }
        });

    }

    私人TextView的createNewTextView(字符串文本){
        最后的LayoutParams lparams =新的LayoutParams(
                LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        最后的TextView TextView的=新的TextView(本);
        textView.setLayoutParams(lparams);
        textView.setText(新文字:+文字);
        返回的TextView;
    }

    私人按钮createNewButton(){
        最后的LayoutParams lparams =新的LayoutParams(
                LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        最后Button按钮=新的按钮(这一点);
        button.setLayoutParams(lparams);
        button.setText( - );
        返回按钮;
    }
  }
 

解决方案

TextViews 按钮堆叠,因为你可能使用的LinearLayout 的方向垂直。你可以换你的的TextView + 按钮的LinearLayout 和然后添加该的LinearLayout 来自己的布局,或者您可以使用 TableLayout 象下面这样(我已经添加了一些ID,以便你可以删除你想要的行):

 公共类SomeActivity延伸活动{

    私人的EditText mInput;
        私人TableLayout mTab​​le;
        私有静态诠释SCOUNT = 0;

        @覆盖
        保护无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.main);
            按钮Add按钮=(按钮)findViewById(R.id.add);
            mInput =(EditText上)findViewById(R.id.editText1);
            mTable =(TableLayout)findViewById(R.id.table1);
            addButton.setOnClickListener(新OnClickListener(){

                @覆盖
                公共无效的onClick(视图v){
                    mTable.addView(addRow(mInput.getText()的toString())。);
                }
            });
        }

        私人的TableRow addRow(String s)将{
            的TableRow TR =新的TableRow(本);
            tr.setId(1000 + SCOUNT);
            tr.setLayoutParams(新TableLayout.LayoutParams(
                    TableLayout.LayoutParams.FILL_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT));
            TableRow.LayoutParams tlparams =新TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            TextView中的TextView =新的TextView(本);
            textView.setLayoutParams(tlparams);
            textView.setText(新文字:+ S);
            tr.addView(TextView的);
            TableRow.LayoutParams blparams =新TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            最后Button按钮=新的按钮(这一点);
            button.setLayoutParams(blparams);
            button.setText( - );
            button.setId(2000 + SCOUNT);
            button.setOnClickListener(新OnClickListener(){

                @覆盖
                公共无效的onClick(视图v){
                    mTable.removeView(findViewById(v.getId() -  1000));
                }
            });
            tr.addView(按钮);
            SCOUNT ++;
            返回TR;
    }

}
 

其中布局文件是:

 < XML版本=1.0编码=UTF-8&GT?;
<滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT>

    <的LinearLayout
        机器人:ID =@ + ID /母
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直>

        <的EditText
            机器人:ID =@ + ID / editText1
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT/>

        <按钮
            机器人:ID =@ + ID /加
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT/>

        < TableLayout
            机器人:ID =@ + ID /表1
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT>
        < / TableLayout>
    < / LinearLayout中>

< /滚动型>
 

如果由于某种原因,你不喜欢 TableLayout 使用的LinearLayout 来包装你的TextView 按钮与上面的布局文件(当然删除 TableLayout

  // ...
LL =(的LinearLayout)findViewById(R.id.parent);
        addButton.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                //其中,LL是ID父的LinearLayout
                ll.addView(addRow(mInput.getText()的toString())。);
            }
        });
    }

    私人的LinearLayout addRow(String s)将{
        的LinearLayout TR =新的LinearLayout(本);
        tr.setId(1000 + SCOUNT);
        tr.setLayoutParams(新LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        LinearLayout.LayoutParams tlparams =新LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        TextView中的TextView =新的TextView(本);
        textView.setLayoutParams(tlparams);
        textView.setText(新文字:+ S);
        tr.addView(TextView的);
        LinearLayout.LayoutParams blparams =新LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        最后Button按钮=新的按钮(这一点);
        button.setLayoutParams(blparams);
        button.setText( - );
        button.setId(2000 + SCOUNT);
        button.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                ll.removeView(findViewById(v.getId() -  1000));
            }
        });
        tr.addView(按钮);
        SCOUNT ++;
        返回TR;
    }
 

I have designed a layout as in the image below:

After entering text in the EditText, when I press the Add+ Button the TextView and Button will be added as shown in the image below:

I want to show the Button on the right side of the TextView. How should I do this? Another question, how should I remove corresponding View when user clicks a button? The code:

 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;
    }
  }

解决方案

The TextViews and Buttons are stacked because you probably use a LinearLayout with the orientation vertical. You could wrap your TextView + Button into a LinearLayout and then add this LinearLayout to your own layout or you could use a TableLayout like below(I've added some ids so you can delete the rows you want):

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;
    }

}

where the main layout file is:

<?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>

If for some reason, you don't like the TableLayout use a LinearLayout to wrap you TextView and Button with the layout file above(and of course remove the 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;
    }

这篇关于如何水平对齐一些编程方式添加的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆