简体   繁体   中英

Programmatically create and align an edittext to the right of a textview

I've built an Android application that programmatically creates a textview and an edittext on the UI when the user presses a button. Currently when you press the 'add' button the two fields are created but the edittext appears underneith the textview, i'd like the edittext to appear on screen aligned to the right of the textview like so:

[addbutton]

[textview][edittext]
[textview][edittext]
[textview][edittext]

code:

private OnClickListener addForm() {

        return new OnClickListener() {

            @Override
            public void onClick(View v) {

                mLayout.addView(createNewTextView(numberString));
                mLayout.addView(createNewEditText(null));
                numberInt = Integer.parseInt(numberString);
                numberInt++;
                numberString = Integer.toString(numberInt);
            }
        };
    }
    // add textview
    private TextView createNewTextView(String text) {
        final TextView textView = new TextView(this);
        textView.setLayoutParams(lparams);
        textView.setText(text);
        return textView;
    }

    // add edittext
    private EditText createNewEditText(String text) {
        final EditText editText = new EditText(this);

        editText.setLayoutParams(lparams);
        editText.setText(text);
        return editText;
    }

REVISION 1:
OK so I've changed my code around a bit to what I think should work but the edittext is still appearing below, can anyone see where i'm going wrong or change my code around to make it work?

New code:

    private OnClickListener add() {

        return new OnClickListener() {

            @Override
            public void onClick(View v) {

                createNewTextView(numberString);

                numberInt = Integer.parseInt(numberString);
                numberInt++;
                numberString = Integer.toString(numberInt);
            }
        };
    }


    // add textview
    private void createNewTextView(String text) {

        final TextView textView = new TextView(this);

        textView.setLayoutParams(lparams);
        textView.setText(text);
        textView.setId(numberInt);

        mLayout.addView(textView);

        createNewEditText(textView);
    }

    // add edittext
    private void createNewEditText(TextView textView) {

        final EditText editText = new EditText(this);


        lparams.addRule(RelativeLayout.RIGHT_OF, textView.getId());
        editText.setLayoutParams(lparams);
        mLayout.addView(editText);

    }

REVISION 2:
All java and XML code below:

Java:

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
private LinearLayout mLayout;
private Button mButton;
private String numberString = "1";
private int numberInt = 1;
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,     LayoutParams.WRAP_CONTENT);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLayout = (LinearLayout) findViewById(R.id.linearLayout);
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(add());
        TextView textView = new TextView(this);
        textView.setText("New text");
    }

    private OnClickListener add() {

        return new OnClickListener() {

            @Override
            public void onClick(View v) {

                mLayout.addView(createNewTextView(numberString));
                mLayout.addView(createNewEditText(null));
                numberInt = Integer.parseInt(numberString);
                numberInt++;
                numberString = Integer.toString(numberInt);
            }
        };
    }

// add textview
    private TextView createNewTextView(String text) {

        final TextView textView = new TextView(this);

        textView.setLayoutParams(lparams);
        textView.setText(text);
        return textView;
    }

    // add edittext
    private EditText createNewEditText(String text) {

    final EditText editText = new EditText(this);

        editText.setLayoutParams(lparams);
        editText.setText(text);
        editText.setId(numberInt);

        int i = editText.getId();

        Toast toast= Toast.makeText(MainActivity.this,"edittext ID:" + i ,      Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, -100);
        toast.show();

        return editText;
    }
}

XML:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout">

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

</LinearLayout>

Use the relative layout as a container and Use relative layout params to align edittext and textview. In the relative layout params there are alignright,alignleft etc.. as in xml. developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

Set id to the TextView and EditText then use RelativeLayout LayoutParams to align right or left using rules like params.addRule(RelativeLayout.ALIGN_LEFT,id);

If your main layout is LinearLayout then make Sure that your main layout orientation is horizontal...and use below code

LayoutParams lparams= new RelativeLayout.LayoutParams(new ViewGroup.MarginLayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));

        TextView textView = new TextView(this);

        textView.setLayoutParams(lparams);
        textView.setText("TextView");
        textView.setId(1);

        mLayout.addView(textView);

        EditText editText = new EditText(this);
        lparams.addRule(RelativeLayout.ALIGN_RIGHT, textView.getId());
        editText.setLayoutParams(lparams);
        mLayout.addView(editText);

使用RelativLayout并将规则设置为android:layout_toRightOf="@+id/textview"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM