ScrollView仅在其子层次结构中显示单个视图

时间:2016-06-11 14:33:44

标签: android android-xml android-scrollview android-relativelayout

我应该添加ScrollView,以便用户在按钮后向下滚动。但是,它没有显示它应该显示的内容。

无论我在Button之后添加什么,都不会显示在设备/模拟器上。我应该在ImageView下添加Button,但它似乎没有在Button之后显示任何内容。

以下是我的XML:

<?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"
    android:fillViewport="true">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
        android:weightSum="1">   
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Description:"
            android:id="@+id/textView14"
            android:paddingTop="20dp"
            android:layout_below="@+id/editText"
            android:layout_alignLeft="@+id/editText"
            android:layout_alignStart="@+id/editText" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Restaurant Location:"
            android:id="@+id/textView15"
            android:paddingTop="20dp"
            android:layout_below="@+id/editText2"
            android:layout_alignLeft="@+id/editText2"
            android:layout_alignStart="@+id/editText2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button2"
            android:layout_below="@+id/imageView3"
            android:layout_centerHorizontal="true" />    
    </RelativeLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:1)

问题是您没有正确对齐视图。例如,没有editText2editTextimageView3,您可以围绕它们的左侧或下方重新对齐您的观点。

RelativeLayout替换为LinearLayout或使用此XML(我已将这些未知ID替换为正确的ID):

<?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"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">   
        <TextView
            android:id="@+id/textView14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Description:"
            android:paddingTop="20dp" />
        <TextView
            android:id="@+id/textView15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Restaurant Location:"
            android:paddingTop="20dp"
            android:layout_below="@+id/textView14" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:layout_below="@id/textView15"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>
</ScrollView>