簡體   English   中英

以編程方式在textview的右側創建並對齊edittext

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

我已經構建了一個Android應用程序,當用戶按下按鈕時,該應用程序可以在UI上以編程方式創建textview和edittext。 當前,當您按下“添加”按鈕時,將創建兩個字段,但是edittext出現在textview的下面,我希望edittext出現在屏幕上,與textview的右側對齊,如下所示:

[Add按鈕]

[TextView的] [的EditText]
[TextView的] [的EditText]
[TextView的] [的EditText]

碼:

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

修訂1:
好的,所以我已經將代碼更改為我認為應該可以使用的代碼,但是edittext仍然顯示在下面,任何人都可以看到我出了問題的地方或更改代碼以使其正常工作嗎?

新代碼:

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

    }

修訂版2:
以下所有Java和XML代碼:

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>

使用相對布局作為容器,並使用相對布局參數來對齊edittext和textview。 在相對布局參數中,有alignright,alignleft等。 developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

將id設置為TextViewEditText然后使用RelativeLayout LayoutParams使用諸如params.addRule(RelativeLayout.ALIGN_LEFT,id)之類的規則左右對齊。

如果您的主布局是LinearLayout則確保您的主布局方向是水平的...並使用以下代碼

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM