Android fields do not work when inserting views dynamically

I have a simple view:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/contact_selected" android:layout_marginTop="10dp" android:layout_marginEnd="5dp" android:orientation="horizontal" android:padding="3dp"> <TextView android:id="@+id/txt_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Billy Bob" /> </LinearLayout> 

When I statically copy LinearLayout markup into my main activity layout, the fields are as expected. However, when I add a view to the main activity layout dynamically, the fields are ignored. This is how I insert the view

 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.test, null); TextView txt_title = (TextView)view.findViewById(R.id.txt_title); txt_title.setText("Dynamic #1"); llayout.addView(view, llayout.getChildCount()-1); View view2 = inflater.inflate(R.layout.test, null); txt_title = (TextView)view2.findViewById(R.id.txt_title); txt_title.setText("Dynamic #2"); llayout.addView(view2, llayout.getChildCount()-1); 

Here's what it looks like:

enter image description here

The container in the main layout is a LinearLayout, which is a child of the HorizontalScrollView. Any insight appreciated.

+7
android dynamic margins
source share
3 answers

When adding views dynamically, you should not inflate the View parent of the null ViewGroup . So, in other words, you should use inflater.inflate(R.layout.test, linearLayout, false); . A parent is used to determine what types of layout parameters should be generated. Pass the parent container (in this case your linear layout) so that it correctly creates ViewGroup.MarginLayoutParams from your XML.

+14
source share

This is because you need to dynamically highlight " Margin ". You can do this by creating a " LayoutPrams " object, for example:

 LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); layoutParams.setMargins(30, 20, 30, 0); 

Here you can set LayoutParams in linelayout:

 ll.addView(okButton, layoutParams); 

Hope this helps.

+2
source share

First, you should get a display density. document related https://developer.android.com/reference/android/util/DisplayMetrics.html

and get the identifier that you want to set for the field field. for my case

 layout_login_box = (ConstraintLayout)findViewById(R.id.login_layout_box); float density = getResources().getDisplayMetrics().density; ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams)layout_login_box.getLayoutParams(); params.setMargins((int) (24 * density),0,(int) (24 * density),(int) (16 * density)); layout_login_box.setLayoutParams(params); 

In addition, you can change ConstraintLayout to your own look.

0
source share

All Articles