【问题标题】:How to add a scroll view to an entire activity?如何将滚动视图添加到整个活动?
【发布时间】:2013-06-19 18:42:17
【问题描述】:

我尝试在活动布局上的所有内容周围添加滚动视图,但它给了我一个错误,说它只能围绕一个东西放置。

我的活动有一个标题文本视图,然后是一个图像,然后是一个描述文本视图,您无法阅读整个描述,因为它太长并且低于我的屏幕边缘。我怎样才能使整个东西可滚动?

我的 xml 看起来像这样:

<?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>

【问题讨论】:

  • 只需在滚动视图中添加您的父线性布局\

标签: java android xml android-activity android-scrollview


【解决方案1】:

试试这个:

<?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>

【讨论】:

    【解决方案2】:

    SrollView(高度为fill_parent)包裹LinearLayout(高度为wrap_content)。

    【讨论】:

      【解决方案3】:

      “一件事”是您的LinearLayout。只需将其包装在 ScrollView 中,您将封装整个活动的布局。

      此外,如果您只想将一个elemtnet 包装在ScrollView 中,您只需将该元素放入它自己的布局中,例如线性或相对布局,然后它就成为您的@987654324 的一个元素@

      【讨论】:

        【解决方案4】:

        就用这个

        <?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>
        

        【讨论】:

        • 在线性布局行我收到此错误:在此行找到多个注释:-元素类型“ScrollView”必须后跟属性规范“>”或“/>”。 - 错误:解析 XML 时出错:格式不正确(无效令牌)
        • 它缺少>,你需要使它 schemas.android.com/apk/res/android" android:id="@+id/scrollview" android:layout_width="fill_parent" android:layout_height ="wrap_content">,也是结尾应该是 而不是 。最后,对于滚动视图,您可能需要添加属性 android:fill_viewport="true" 以便您的滚动视图覆盖整个屏幕。
        • 是的,在编辑时错过了,但现在应该可以正常工作了 :)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-10
        相关资源
        最近更新 更多