Jak wyrównać poziomo niektóre widoki dodane programowo?
Zaprojektowałem układ jak na obrazku poniżej:
Po wprowadzeniu tekstu wEditText
, kiedy naciskamDodaj + Button
TextView
iButton
zostanie dodany jak pokazano na obrazku poniżej:
Chcę pokazaćButton
po prawej stronieTextView
. Jak mam to zrobić? Kolejne pytanie, jak mam usunąć odpowiednieView
kiedy użytkownik kliknie przycisk? Kod:
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;
}
}