【问题标题】:Programmatically create and align an edittext to the right of a textview以编程方式创建编辑文本并将其对齐到文本视图的右侧
【发布时间】:2014-04-10 10:15:03
【问题描述】:

我构建了一个 Android 应用程序,当用户按下按钮时,它以编程方式在 UI 上创建一个 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>

【问题讨论】:

    标签: java android user-interface layout alignment


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      使用相对布局作为容器并使用相对布局参数来对齐edittext和textview。在相对布局参数中,有 alignright、alignleft 等。如在 xml 中。 developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 如何设置他们的id??
        • 喜欢这个 textView.setId(1);
        • 还有问题吗?
        • 抱歉,我认为它可以工作,但看起来他们仍然坐在彼此之上而不是并排。我相信这应该很简单
        • 是否可以发布您的 xml 和完整的活动代码,以便我可以帮助...
        猜你喜欢
        • 2016-03-13
        • 1970-01-01
        • 1970-01-01
        • 2014-04-26
        • 2023-03-14
        • 2013-07-16
        • 2016-01-12
        • 2016-10-29
        • 2020-10-26
        相关资源
        最近更新 更多