Anyone help me to modify the layout params of a child in a ListView in Android.Please give some code snippets if you can.
ViewGroup.LayoutParams has only two attributes: height and width. It’s subclass - ViewGroup.MarginLayoutParams inherits these two attributes and has four of its own: bottomMargin, leftMargin, rightMargin, topMargin.
In layout-files a root element is usually LinearLayout, so we will use it too. Update the imports CTRL+SHIFT+O. Eclipse will ask which exactly LayoutParams we want to use. We will explore this in more detail. Let’s recall the theory about screens. A screen consists of the ViewGroup and View elements inside it.
//keep in mind that LinearLayout is not the only Layout, e.g. you can use ConstrainLayout etc... //after, you need to add yourView to a layout as it won't redraw itself if you just change the layout parameters e.g.:
Here is a long series of blog posts that I found really useful when I started to create my first custom ListView.
NOTE: every word is a separate link ;)
The childs need the LayoutParams from the parent so:
First - The inflater:
convertView = this.inflater.inflate(R.layout.row_main_menu,parent,false);
Look the 2 and 3 parameter.
Second - The LayoutParams:
The ListView LayoutParams not appear in eclipse intellisense! Try this:
ListView.LayoutParams params = new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT,ListView.LayoutParams.WRAP_CONTENT);
convertView.setLayoutParams(params);
Write your own Adapter class and overwrite the getView method. The getView method is called for each position in the list.
You'll have to overwrite some other methods too as given here
You can create custom views and return them from the getView method. Remember to use the older view passed down to the method if its possible for optimum memory usage
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With