Java 如何向整个活动添加滚动视图?
我尝试在活动布局上的所有内容周围添加一个滚动视图,但它给了我一个错误,即它只能放置在一个内容周围 我的活动,有一个标题文本视图,然后是一个图像,然后是一个描述文本视图,你不能阅读整个描述,因为它太长,并且在我的屏幕边缘下方。我怎样才能使整个东西滚动 我的xml如下所示:Java 如何向整个活动添加滚动视图?,java,android,xml,android-activity,android-scrollview,Java,Android,Xml,Android Activity,Android Scrollview,我尝试在活动布局上的所有内容周围添加一个滚动视图,但它给了我一个错误,即它只能放置在一个内容周围 我的活动,有一个标题文本视图,然后是一个图像,然后是一个描述文本视图,你不能阅读整个描述,因为它太长,并且在我的屏幕边缘下方。我怎样才能使整个东西滚动 我的xml如下所示: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/beerTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="40sp"
android:textStyle = "bold"
>
</TextView>
<ImageView android:id="@+id/image"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_margin="10dip"/>
<Button
android:id="@+id/buttonBrewery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="@+id/buttonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/beerDEscriptionTitle"
android:textStyle = "bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:text="Description"
></TextView>
<TextView
android:id="@+id/beerDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="15sp"
></TextView>
</LinearLayout>
一件事是你的线性布局。只需将其包装在滚动视图中
,即可封装整个活动的布局
此外,如果您只想在滚动视图中包装一个elemtnet,您所要做的就是将该元素放入它自己的布局中,就像线性或相对布局一样,然后它就成为滚动视图的一个元素包装线性布局
(高度为包装内容
)使用SrollView
(高度为fill\u parent
)。只需使用此选项即可
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/beerTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="40sp"
android:textStyle = "bold"
>
</TextView>
<ImageView android:id="@+id/image"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_margin="10dip"/>
<Button
android:id="@+id/buttonBrewery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="@+id/buttonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/beerDEscriptionTitle"
android:textStyle = "bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:text="Description"
></TextView>
<TextView
android:id="@+id/beerDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="15sp"
></TextView>
</LinearLayout>
</ScrollView>
试试这个:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- TextView and other stuff -->
</LinearLayout>
</ScrollView>
只需将父线性布局添加到滚动视图中\在线性布局行中,我发现以下错误:在该行中找到多个注释:-元素类型“ScrollView”后面必须跟属性规范“>”或“/>”。-错误:解析XML时出错:格式不正确(无效令牌)。它缺少>,您需要创建它,并且结尾不应该是。最后,对于scrollview,您可能需要添加属性android:fill\u viewport=“true”,这样您的scrollview就可以覆盖整个屏幕。是的,在编辑时错过了这个属性,但现在应该可以正常工作了:)