Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - Alignment of dynamic views

I want the alignment of the editext and the imageviewin the same horizontal line but I am getting the imageview below the edittext.

main fragment code

 final LinearLayout lnrView = (LinearLayout) 
 child.findViewById(R.id.lnrView);
                Button btnad = (Button) child.findViewById(R.id.btnad);
                btnad.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        lnrView.addView(newedittext());
                        lnrView.addView(newImageview(getActivity()));
                    }
                });

neweditext method

  private View newedittext() {
    final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    final EditText editText = new EditText(getActivity());
    int id = 0;
    editText.setId(id);
    editText.setTextSize(14);
    editText.setTextColor(Color.rgb(0, 0, 0));
    editText.setMaxEms(2);
    editText.setInputType(InputType.TYPE_CLASS_TEXT);
    return editText;
}

newImageview method

 public ImageView newImageview(Context context) {
    final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    final ImageView imgView = new ImageView(context);
    int id = 0;
    imgView.setId(id);
    imgView.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_close_red));
    return imgView;
}

main fragment xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
    android:id="@+id/btnad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="add" />

<LinearLayout
    android:id="@+id/lnrView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


</LinearLayout>

</LinearLayout>

when i click the Add button an edittext and Imageview is created dynamically and my output screen look like below

enter image description here

i want the imageview and the edittext in the same line. can anyone help me.

when i add "Horizontal" this is the output

enter image description here

this my expected result

enter image description here

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You need add first a Horizontal Linearlayout in Your lnrView than add EditText and Imageview

Try this

 button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            LinearLayout layout = new LinearLayout(MainActivity.this);
            layout.setOrientation(LinearLayout.HORIZONTAL);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);

            layout.setLayoutParams(lp);
            layout.addView(newedittext());
            layout.addView(newImageview(MainActivity.this));

            lnrView.addView(layout);
        }
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...