如何将ScrollView添加到Android布局

时间:2013-02-01 19:34:35

标签: android android-layout android-widget xamarin.android

我有以下布局但如果内容对于显示来说太多,我想让它垂直滚动。 ImageView,'imagePhoto'是使用设备相机拍摄的图像。如果图片是肖像然后布局需要能够滚动。我尝试在LinearLayout之前添加ScrollView,如下所示:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

但它似乎没有用。

有什么建议吗?

这是布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widgetUploadPhoto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/topLevelMenuBackground"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tvHeaderText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text=""
            android:textColor="#ffffffff"
            android:textSize="20sp"
            android:shadowColor="#7F000000"
            android:shadowDx="1"
            android:shadowDy="1"
            android:shadowRadius="2"
            android:layout_centerVertical="true" />
    </RelativeLayout>
    <TextView
        android:id="@+id/tvYourCurrentPhoto"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:text="Your current photo:"
        android:background="#ffffffff"
        android:textColor="#ff626262"
        android:paddingTop="10dp" 
        android:paddingLeft="10dp"
        android:paddingBottom="10dp"
        android:textSize="@dimen/text_size_medium"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginBottom="0dp"
        android:visibility="gone" />
    <ImageView
        android:id="@+id/imagePhoto"
        android:src="@drawable/OrangeAccessoryButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center_horizontal"
        android:background="#ffffffff"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
        android:paddingTop="15dp"
        android:paddingRight="15dp"
        android:paddingBottom="15dp"
        android:paddingLeft="15dp"
        android:visibility="gone" />
    <TextView
        android:id="@+id/empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:text="Select an option below to change your photo."
        android:background="#ffffffff"
        android:textColor="#ff626262"
        android:paddingTop="15dp"
        android:paddingRight="15dp"
        android:paddingBottom="15dp"
        android:paddingLeft="15dp"
        android:textSize="@dimen/text_size_medium" />
    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:focusable="false"
        android:layout_weight="1" />
</LinearLayout>

感谢您提供任何帮助/建议。

更新的XML:

注意:这有效,但不是预期的结果。我只想要在RelativeView中滚动东西。第一个TexView是一个标题,我想保持静止,下面的视图可以滚动。基本上,一切都应该滚动,但是相对的部分。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/widgetUploadPhoto"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffffff"
        android:orientation="vertical">
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/topLevelMenuBackground"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tvHeaderText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:text=""
                android:textColor="#ffffffff"
                android:textSize="20sp"
                android:shadowColor="#7F000000"
                android:shadowDx="1"
                android:shadowDy="1"
                android:shadowRadius="2"
                android:layout_centerVertical="true" />
        </RelativeLayout>
        <TextView
            android:id="@+id/tvYourCurrentPhoto"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="Your current photo:"
            android:background="#ffffffff"
            android:textColor="#ff626262"
            android:paddingTop="10dp" 
            android:paddingLeft="10dp"
            android:paddingBottom="10dp"
            android:textSize="@dimen/text_size_medium"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="0dp"
            android:layout_marginRight="0dp"
            android:layout_marginBottom="0dp"
            android:visibility="gone" />
        <ImageView
            android:id="@+id/imagePhoto"
            android:src="@drawable/OrangeAccessoryButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="center_horizontal"
            android:background="#ffffffff"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="0dp"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="0dp"
            android:paddingTop="15dp"
            android:paddingRight="15dp"
            android:paddingBottom="15dp"
            android:paddingLeft="15dp"
            android:visibility="gone" />
        <TextView
            android:id="@+id/empty"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="Select an option below to change your photo."
            android:background="#ffffffff"
            android:textColor="#ff626262"
            android:paddingTop="15dp"
            android:paddingRight="15dp"
            android:paddingBottom="15dp"
            android:paddingLeft="15dp"
            android:textSize="@dimen/text_size_medium" />
        <ListView
            android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:focusable="false"
            android:layout_weight="1" />
    </LinearLayout>
</ScrollView>

4 个答案:

答案 0 :(得分:2)

这将有一个标题TextView,它不会在顶部滚动,然后是在垂直滚动下方的任何内容。请特别注意ScrollView的{​​{1}}属性,android:height="0dp"android:weight="1"的组合将使其展开,以便在绘制顶部TextView后填充所有剩余空间。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical>
    <TextView android:id="@+id/tvHeaderText"
        android:background="@drawable/topLevelMenuBackground"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textColor="#ffffffff"
        android:textSize="20sp"
        android:shadowColor="#7F000000"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="2" />
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <!-- Everything you want to scroll goes here -->
        </LinearLayout>
    </ScrollView>
</LinearLayout>

答案 1 :(得分:0)

试试这个

    <?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 xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widgetUploadPhoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
android:orientation="vertical">
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/topLevelMenuBackground"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/tvHeaderText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:text=""
        android:textColor="#ffffffff"
        android:textSize="20sp"
        android:shadowColor="#7F000000"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="2"
        android:layout_centerVertical="true" />
</RelativeLayout>
<TextView
    android:id="@+id/tvYourCurrentPhoto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:text="Your current photo:"
    android:background="#ffffffff"
    android:textColor="#ff626262"
    android:paddingTop="10dp" 
    android:paddingLeft="10dp"
    android:paddingBottom="10dp"
    android:textSize="@dimen/text_size_medium"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginBottom="0dp"
    android:visibility="gone" />
<ImageView
    android:id="@+id/imagePhoto"
    android:src="@drawable/OrangeAccessoryButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_gravity="center_horizontal"
    android:background="#ffffffff"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="0dp"
    android:layout_marginBottom="0dp"
    android:paddingTop="15dp"
    android:paddingRight="15dp"
    android:paddingBottom="15dp"
    android:paddingLeft="15dp"
    android:visibility="gone" />
<TextView
    android:id="@+id/empty"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:text="Select an option below to change your photo."
    android:background="#ffffffff"
    android:textColor="#ff626262"
    android:paddingTop="15dp"
    android:paddingRight="15dp"
    android:paddingBottom="15dp"
    android:paddingLeft="15dp"
    android:textSize="@dimen/text_size_medium" />
<ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:focusable="false"
    android:layout_weight="1" />

</ScrollView>

答案 2 :(得分:0)

结构如下:

<LinearLayout>
    <RelativeLayout>
        <TextView android:id="@+id/tvHeaderText" />
    </RelativeLayout>
    <ScrollView>
        <LinearLayout android:id="@+id/widgetUploadPhoto" >
            <TextView android:id="@+id/tvYourCurrentPhoto"/>
            <ImageView android:id="@+id/imagePhoto" />
            <TextView android:id="@+id/empty"/>
            <ListView android:id="@+id/listView"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

首先,以LinearLayout为根。然后移动您希望它始终位于顶部的根RelativeLayout下的LinearLayout。接下来,在ScrollView之后加RelativeLayout,然后将另一个LinearLayout作为孩子ScorllView

另外,我不认为在ListView内加ScrollView是个好主意。请参阅answer

答案 3 :(得分:0)

您在手机或平板电脑上测试哪种设备?请注意,手机上的功能可能无法在平板电脑上运行,因此对于手机和sw600dp-land,您必须进行测试和自定义。我已经为图表实现了Scrollview,我在这里分享我的工作代码。

 <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ScrollView01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="20sp">

        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tableLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:background="@drawable/back"
            android:scrollbars="vertical"
            android:textSize="20sp">
            <!--Month Combo Box-->
            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="20sp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Select Month"
                    android:textColor="#0c0b0b"
                    android:textSize="20sp" />

                <Spinner
                    android:id="@+id/spinner"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#0c0b0b"
                    android:textSize="20sp" />

                <Spinner
                    android:id="@+id/spinneryr"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#0c0b0b"
                    android:textSize="20sp" />
            </TableRow>
            <!--Field1 Combo Box-->
            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="20sp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/ERY"
                    android:textColor="#0c0b0b"
                    android:textSize="20sp" />


                <EditText
                    android:id="@+id/ERYS"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/back"
                    android:gravity="center"
                    android:inputType="numberDecimal" />
            </TableRow>

            <!--Field9 Combo Box-->
            <TableRow
                android:id="@+id/tableRow10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="20sp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/PCOUNT"
                    android:textColor="#0c0b0b"
                    android:textSize="20sp" />

                <EditText
                    android:id="@+id/PLS"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/back"
                    android:gravity="center"
                    android:inputType="numberDecimal" />
            </TableRow>
            <!--Field10 Combo Box-->
            <TableRow
                android:id="@+id/tableRow11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="20sp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/NES"
                    android:textColor="#0c0b0b"
                    android:textSize="20sp" />

                <EditText
                    android:id="@+id/NESSS"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/back"
                    android:gravity="center"
                    android:inputType="numberDecimal" />
            </TableRow>
            <!--Field11 Combo Box-->
            <TableRow
                android:id="@+id/tableRow12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="20sp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/EOS"
                    android:textColor="#0c0b0b"
                    android:textSize="20sp" />

                <EditText
                    android:id="@+id/EOSS"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/back"
                    android:gravity="center"
                    android:inputType="numberDecimal" />
            </TableRow>
            <!--Field12 Combo Box-->
            <!--Removed android:layout_gravity="center"-->
            <TableRow
                android:id="@+id/tableRow13"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="20sp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/LYMS"
                    android:textColor="#0c0b0b"
                    android:textSize="20sp" />

                <EditText
                    android:id="@+id/LYMSS"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/back"
                    android:gravity="center"
                    android:inputType="numberDecimal" />
            </TableRow>
            <!-- Button :Start here-->
            <TableRow
                android:id="@+id/tableRow14"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="20sp">

                <Button
                    android:id="@+id/btnAdd"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/add" />
                <Button
                    android:id="@+id/btnViewAll"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/view_all" />

            </TableRow>

            <TextView
                android:id="@+id/tvValues"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="20sp" />
        </TableLayout>
    </ScrollView>