ListView element will not expand width to fill_parent

I have a custom ViewGroup that inflates a ListView from an xml layout. The layout of the list item is pumped from another xml file. All views are set to fill_parent . ListView fills the parent element, but the ListView elements do not matter.

I tried putting the ListView in LinearLayout and assigning it a weight. Tried RelativeLayout . In addition, I built the ListView program code without using the xml layout. Even modified LayoutParams before adding the view to the ViewGroup .

I also took these messages into account: Width of clickable area in ListView w / onListItemClick , In Android, how can I set the height and width of a ListView element? , Simple ListView screen width .

Any ideas why the elements do not extend over the fill width? And how to expand them?

MyViewGroup Class:

 public class MyViewGroup extends ViewGroup { public MyViewGroup(Context context, AttributeSet attrs) { super(context, attrs); generateMyViewGroup(); } private void generateMyViewGroup() { ListView main = (ListView) View.inflate(getContext(), R.layout.layout_main, null); main.setAdapter(new MyAdapter(getContext())); this.addView(main); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { this.getChildAt(0).layout(l, t, r, b); } } 

ListView xml layout:

 <?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginRight="3dp" android:background="#77000000" android:cacheColorHint="#00000000" android:divider="#00000000" android:dividerHeight="0dp" android:drawSelectorOnTop="false" android:scrollbars="vertical" > </ListView> 

ListView item location:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_main_category" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/mainBackground" android:gravity="fill_horizontal|center_vertical" android:orientation="vertical" > <TextView android:id="@+id/main_category" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:paddingBottom="7dp" android:paddingLeft="20dp" android:paddingRight="5dp" android:paddingTop="20dp" android:text="test" android:textColor="@color/mainCategory" android:textSize="15sp" android:textStyle="bold" /> </LinearLayout> 

Part of the MyAddapter class:

 public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if(null == convertView) { LayoutInflater inflater = LayoutInflater.from(context); convertView = inflater.inflate(R.layout.layout_main_category, null); viewHolder = new ViewHolder(); viewHolder.mainItemTitle = (TextView) convertView.findViewById(R.id.main_category); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.mainItemTitle.setText("text"); return convertView; } private static class ViewHolder { public TextView menuItemTitle; } 
+4
source share
2 answers

ViewGroup not working properly. I also tried using LinearLayout . But RelativeLayout finished correctly inflating the layout. So, my xml layouts and adapter remained unchanged, but I changed the main class to the RelativeLayout extension. Also remove onLayout() .

MyView Class:

 public class MyView extends RelativeLayout { //Extra code unchanged .... private void generateMyView() { ListView menu = (ListView) View.inflate(getContext(), R.layout.layout_main, null); menu.setAdapter(new MyAdapter(getContext())); this.addView(menu); } //Removed onLayout() } 
0
source

If you override onLayout , you should also override onMeasure .. almost always ..

 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ int parentWidth = MeasureSpec.getSize(widthMeasureSpec); int parentHeight = MeasureSpec.getSize(heightMeasureSpec); this.setMeasuredDimension(parentWidth, parentHeight); // this.setLayoutParams(new // *ParentLayoutType*.LayoutParams(parentWidth,parentHeight)); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } 

If it still doesnโ€™t work, also use a commented line.

Update:

Another option for you is to forget onLayout and onMeasure and override onFinishInflate() and add these lines to it. If you MyViewGroup from xml.

 protected void onFinishInflate () { super.onFinishInflate(); ListView main = (ListView) View.inflate(getContext(), R.layout.layout_main,null); main.setAdapter(new MyAdapter(getContext())); this.addView(main); } 
0
source

Source: https://habr.com/ru/post/1435964/


All Articles