问题描述
我是android编程的新手,horizontalscrollview有问题.我在错误此HorizontalScrollView布局或其LinearLayout父级无用"上
I am new in android programming I have problem with horizontalscrollview. I have error "This HorizontalScrollView layout or its LinearLayout parent is useless" on
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="lv.myproject.formula.MainFormula"
tools:ignore="MergeRootFrame"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/bgtest">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
推荐答案
首先,这不是错误,而是警告-代码仍然可以正常工作.
First of all, this is not an error but a warning - the code will still work correctly.
现在,如您所见,您已经拥有:
Now, as you can see, you have:
<LinearLayout ...>
<HorizontalScrollView ...>
</HorizontalScrollView ...>
</LinearLayout>
LinearLayout
中只有一个孩子-另一个ViewGroup
-因此您可以摆脱外部LinearLayout
并只需保留HorizontalScrollView
:
There is only one child in the LinearLayout
- and it's another ViewGroup
- therefore you can get rid of the outer LinearLayout
and simply keep the HorizontalScrollView
:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="lv.myproject.formula.MainFormula"
tools:ignore="MergeRootFrame"
android:layout_marginTop="50dp" >
...
</HorizontalScrollView>
但是,如果您要在代码中的LinearLayout
中添加任何视图(即作为HorizontalScrollView
的同级对象),则正确的做法是忽略此警告.
However if you are adding any Views to the LinearLayout
in your code (i.e. as siblings to your HorizontalScrollView
) then the correct thing is to ignore this warning.
这篇关于此HorizontalScrollView布局或其LinearLayout父级是无用的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!