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

Categories

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

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

1 Answer

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);
        }
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...