我已经构建了一个Android应用程序,当用户按下按钮时,该应用程序可以在UI上以编程方式创建textview和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:
好的,所以我已经将我的代码改为一些我觉得应该工作的但是编辑文本仍然出现在下面,任何人都可以看到我出错的地方或改变我的代码以使其工作吗?
新代码:
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代码:
爪哇:
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>
答案 0 :(得分:1)
将相对布局用作容器,并使用相对布局参数来对齐edittext和textview。在相对布局参数中,有对齐,对齐等等。如xml中所示。 developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
答案 1 :(得分:1)
将ID设置为TextView
和EditText
,然后使用RelativeLayout
LayoutParams
使用params.addRule(RelativeLayout.ALIGN_LEFT,id)等规则左右对齐; < / p>
如果您的主要布局是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);
答案 2 :(得分:1)
使用RelativLayout
并将规则设置为android:layout_toRightOf="@+id/textview"