HorizontalScrollView can contain only one direct child

I have a relative layout and adding an image programmatically in my horizontal scroll that fits in xml.when I tried adding my image to horizontalScrollView..im, getting an exception at runtime. HorizontalScrollView can only contain one child.could you guys help to me

RelativeLayout.LayoutParams HParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); HParams.topMargin = 200 * displayHeight / 480; HsrollView.setLayoutParams(HParams); for (int i = 0; i < 4; i++) { ImageView btnTag = new ImageView(this); btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); btnTag.setImageResource(R.drawable.book); btnTag.setTag(i); btnTag.setId(i); HsrollView.addView(btnTag); } 

Xml file

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/directbg" tools:context=".DirectorActivity" > <HorizontalScrollView android:id="@+id/Hscrollview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scrollbars="none"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > </LinearLayout> </HorizontalScrollView> </RelativeLayout> 
+6
source share
3 answers

Meaning, you should add an image to linear mode. when you add a view of the image, you add it to the HorizontalScrollview , which also has a LinearLayout in it ther, adding 2 children to the HorizontalScrollView, which you cannot do

+7
source

You should add your buttons to your LinearLayout , not directly to the HorizontalScrollView . As the error indicates, a HorizontalScrollView can have only one child.

The best way to do this is to provide your LinearLayout identifier and specify a LinearLayout in your code instead of a HorizontalScrollView .

+3
source

The error tells you everything you need. A ScrollView can only have one child, and in your xml layout you already have LinearLayout inside ScrollView , so you just need to add your images to LinearLayout instead of ScrollView .

+1
source

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


All Articles