[英]java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
I want to realize the Gallery using HorizontalScrollView.我想使用 HorizontalScrollView 来实现 Gallery。 But it throws java.lang.illegalStateException.
但它会抛出 java.lang.illegalStateException。 The logcat hint that the HorizontalScrollView can host only one direct child.
logcat 提示 HorizontalScrollView 只能承载一个直接子级。 I only set one direct child - LinearLayout to it.
我只给它设置了一个直接子元素——LinearLayout。 I searched from the Internet, but haven't solved the problem.
我从网上搜索,但没有解决问题。 Hope anyone can help me.
希望任何人都可以帮助我。 Belows are the code.
下面是代码。
xml xml
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:scrollbars="none"
>
<LinearLayout
android:id="@+id/gallery_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
item xml项目xml
<?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:orientation="vertical"
android:paddingTop="10dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="20dp"
>
<ImageView
android:id="@+id/trend_item_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:src="@drawable/gallery0"
/>
<TextView
android:id="@+id/trend_item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="权利的游戏"
android:textSize="16sp"
/>
</LinearLayout>
Fragment分段
public class DiscoverFragment extends Fragment {
private int[] imageResources = {R.drawable.gallery0, R.drawable.gallery1, R.drawable.gallery2,
R.drawable.gallery3, R.drawable.gallery4, R.drawable.gallery5};
private String[] imageDescriptions = {"权利的游戏", "风吹的风景", "插画背景", "美食君", "吃", "你好四月"};
private List<TrendItem> trendItemList;
private LinearLayout galleryLayout;
public DiscoverFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_discover, container, false);
ButterKnife.bind(this, view);
galleryLayout = ButterKnife.findById(view, R.id.gallery_layout);
initData();
setView();
return view;
}
private void initData(){
trendItemList = new ArrayList<>();
for (int i = 0; i < imageResources.length; i++){
TrendItem trendItem = new TrendItem();
trendItem.setImageResource(imageResources[i]);
trendItem.setText(imageDescriptions[i]);
trendItemList.add(trendItem);
}
}
private void setView(){
for (int i = 0; i < trendItemList.size(); i++){
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.trend_item, galleryLayout, false);
ImageView trendItemImageView = ButterKnife.findById(view, R.id.trend_item_imageview);
TextView trendItemTextView = ButterKnife.findById(view, R.id.trend_item_textview);
trendItemImageView.setImageResource(trendItemList.get(i).getImageResource());
trendItemTextView.setText(trendItemList.get(i).getText());
galleryLayout.addView(view);
}
}
}
Meaning, you have to add the ImageView
, TextView
to the Linearlayout
.意思是,您必须将
ImageView
、 TextView
添加到Linearlayout
。 when you add the imageview you are adding it to the HorizontalScrollview
which also has a LinearLayout
in it their by adding 2 child elements to the HorizontalScrollView which you cannot do.当您添加 imageview 时,您将它添加到
HorizontalScrollview
,该LinearLayout
中也有一个LinearLayout
,通过向 HorizontalScrollView 添加 2 个子元素,这是您无法做到的。
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:scrollbars="none">
<LinearLayout
android:id="@+id/gallery_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/trend_item_imageview"
android:layout_width="your_size"
android:layout_height="your_size"
android:src="@drawable/gallery0"/>
<TextView
android:id="@+id/trend_item_textview"
android:layout_width="your_size"
android:layout_height="your_size"
android:text="权利的游戏"
android:textSize="16sp"/>
</LinearLayout>
</HorizontalScrollView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.