我有附件文件列表,想让我的LinearLayout水平滚动。我只将一个子LinearLayout添加到我的HorizontalScrollView,除了我得到IllegalStateException。我的xml:
<HorizontalScrollView
android:id="@+id/scrollMessageFiles"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:layout_below="@+id/editMessage"
android:orientation="horizontal"
android:weightSum="1.0" >
<LinearLayout
android:id="@+id/panelMessageFiles"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#FFFFFF"
>
</LinearLayout>
</HorizontalScrollView>
并想要添加文件列表 ScrollView中的LinearLayout如下:
public void addFiles()
{
HorizontalScrollView scroll = (HorizontalScrollView) findViewById(R.id.scrollMessageFiles);
LinearLayout layout = (LinearLayout) findViewById(R.id.panelMessageFiles);
if(!FileManagerActivity.getFinalAttachFiles().isEmpty())
{
for (File file: FileManagerActivity.getFinalAttachFiles())
{
View line = new View(this);
line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
line.setBackgroundColor(0xAA345556);
informationView = new TextView(this);
informationView.setTextColor(Color.BLACK);
informationView.setTextSize(12);
informationView.setCompoundDrawablesWithIntrinsicBounds(
0, R.drawable.file_icon, 0, 0);
informationView.setText(file.getName().toString());
layout.addView(informationView, 0);
layout.addView(line, 1);
}
scroll.addView(layout);
}
}
除了get
之外,我只向HorizontalScrollView添加一个LinearLayout FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assignmentexpert/com.assignmentexpert.NewMessageActivity}: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
at android.widget.HorizontalScrollView.addView(HorizontalScrollView.java:223)
at com.assignmentexpert.NewMessageActivity.addFiles(NewMessageActivity.java:165)
at com.assignmentexpert.NewMessageActivity.onCreate(NewMessageActivity.java:90)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
在
行 scroll.addView(layout);
答案 0 :(得分:5)
你只需要删除这一行:scroll.addView(layout);
你已经在xml中声明了这个并且正在尝试再次添加它,这就是你得到多个子例外的原因。
尝试运行:
public void addFiles() {
HorizontalScrollView scroll = (HorizontalScrollView) findViewById(R.id.scrollMessageFiles);
LinearLayout layout = (LinearLayout) findViewById(R.id.panelMessageFiles);
if(!FileManagerActivity.getFinalAttachFiles().isEmpty()) {
for (File file: FileManagerActivity.getFinalAttachFiles()) {
View line = new View(this);
line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
line.setBackgroundColor(0xAA345556);
informationView = new TextView(this);
informationView.setTextColor(Color.BLACK);
informationView.setTextSize(12);
informationView.setCompoundDrawablesWithIntrinsicBounds(
0, R.drawable.file_icon, 0, 0);
informationView.setText(file.getName().toString());
layout.addView(informationView, 0);
layout.addView(line, 1);
}
// This line is telling the system to add your LinearLayout to the ScrollView when it is already there, declared in your xml layout file
//scroll.addView(layout);
}
}
答案 1 :(得分:3)
我决定只删除scroll.addView(layout);
答案 2 :(得分:3)
在尝试添加新内容之前删除旧孩子。尝试:
scroll.removeAllViews();
前
scroll.addView(layout);
答案 3 :(得分:3)
<HorizontalScrollView
android:id="@+id/btnholder"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="131dp"
android:scrollbars="horizontal" >
<LinearLayout
android:id="@+id/holderscroll"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_marginTop="2dp" >
</LinearLayout>
</HorizontalScrollView>
subcategoryscrollV =(HorizontalScrollView)findViewById(R.id.btnholder);
addbtnlinearlay =(LinearLayout)findViewById(R.id.holderscroll);
在布局上添加按钮:
addbtnlinearlay.addView(BTN [I]);
答案 4 :(得分:2)
我很抱歉,但我甚至懒得阅读你的代码,错误非常明确:
HorizontalScrollView can host only one direct child
不要在水平滚动视图中放置多个子项! 在那里放置一些groupview(线性布局,相对布局,无论你想要什么),并在那个组中放置孩子。
答案 5 :(得分:2)
ScrollView只能有一个孩子,因此直接添加更多子项是没有意义的。让我们说你的ScrollView里面有一个LinearLayout,然后你可以为LinearLayout添加更多的视图: