我设计了如下图所示的布局:
在EditText
中输入文字后,当我按添加+ Button
时,TextView
和Button
将会添加,如下所示图片如下:
我想在Button
的右侧显示TextView
。我该怎么做?
另一个问题是,当用户点击按钮时,如何删除相应的View
?代码:
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;
}
}
答案 0 :(得分:1)
LinearLayout具有属性Orientation以垂直/水平对齐控件 所以只需设置相同的方向
http://developer.android.com/reference/android/widget/LinearLayout.html
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mLayout.setOrientation(LinearLayout.HORIZONTAL);
更新
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="schemas.android.com/apk/res/android";
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editText"
android:layout_width="293dp"
android:layout_height="wrap_content" >
<requestFocus /> </EditText>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add+" />
</LinearLayout>
答案 1 :(得分:1)
TextViews
和Buttons
已堆叠,因为您可能使用方向为LinearLayout
的{{1}}。您可以将vertical
+ TextView
打包到Button
,然后将此LinearLayout
添加到您自己的布局中,或者您可以使用下面的LinearLayout
(我'我添加了一些ID,因此您可以删除所需的行:
TableLayout
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;
}
}
布局文件为:
main
如果由于某种原因,您不希望<?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>
使用TableLayout
将LinearLayout
和TextView
用上面的布局文件包裹起来(当然要删除Button
):
TableLayout