Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

android - How to horizontally align some programmatically added views?

I have designed a layout as in the image below:

image

After entering text in the EditText, when I press the Add+ Button the TextView and Button will be added as shown in the image below:

image

I want to show the Button on the right side of the TextView. How should I do this? Another question, how should I remove corresponding View when user clicks a button? The code:

 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;
    }
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

LinearLayout having the property Orientation to align control either Vertically/Horizontally so just set Orientation of the same

http://developer.android.com/reference/android/widget/LinearLayout.html

 mLayout = (LinearLayout) findViewById(R.id.linearLayout);
 mLayout.setOrientation(LinearLayout.HORIZONTAL);

Updated

<?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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...