简体   繁体   English

滚动视图不适用于相对布局

[英]Scrollview not working for Relative Layout

I am trying to populate a Relative layout with 4 card views in it. 我正在尝试在相对布局中填充4张卡片视图。 Each cardview consists one image and a text view. 每个cardview包含一个图像和一个文本视图。 I wrapped all the card views in a relative layout and the relative layout in a scroll view. 我将所有卡片视图包装在相对布局中,并将相对布局包装在滚动视图中。 It's not working. 没用 If I remove scrollview, its working fine. 如果我删除滚动视图,它的工作正常。 Tried by putting scrollview inside the relative layout. 通过将scrollview放在相对布局中进行了尝试。 But didnt work Code: 但没有工作代码:

    <?xml version="1.0" encoding="utf-8"?>
    <Scrollview
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:fillViewport="true"
     android:layout_height="match_parent">

   <RelativeLayout
      android:id="@+id/activity_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#659D32"
      android:paddingBottom="16dp"
      android:paddingLeft="16dp"
      android:paddingRight="16dp"
      android:orientation="vertical"
      android:paddingTop="16dp"
      tools:context="com.example.MainActivity">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:cardCornerRadius="5dp"
    android:id="@+id/cardview_1"
    app:cardElevation="10dp"
    app:cardMaxElevation="15dp">

    <RelativeLayout
        android:layout_width="match_parent"

        android:layout_height="match_parent">


        <ImageView
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:src="@drawable/img_1"
            android:id="@+id/cardImage_bang"
            android:scaleType="centerCrop"/>

        <TextView
            android:layout_below="@+id/cardImage_1"
            android:id="@+id/cardTitle_1"
            android:gravity="center_horizontal"
            android:text="Text1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </RelativeLayout>
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:cardCornerRadius="5dp"
    android:layout_below="@+id/cardview_1"
    android:id="@+id/cardview_2"
    app:cardElevation="10dp"
    android:layout_marginTop="10dp"

    app:cardMaxElevation="15dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <ImageView
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:src="@drawable/img_2"
            android:id="@+id/cardImage_2"
            android:scaleType="centerCrop"/>

        <TextView
            android:layout_below="@+id/cardImage_2"
            android:id="@+id/cardTitle_2"
            android:gravity="center_horizontal"
            android:text="Text2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>

Hope this will help you.. (this snippet worked in my case) 希望这会对您有所帮助。(此片段在我的情况下有效)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:paddingLeft="5dp"
    android:background="@color/bg"
    android:paddingRight="5dp">

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:orientation="vertical"
            android:padding="10dp"
            android:weightSum="1">

        <!-- put entire thing here -->

    </RelativeLayout>
</ScrollView>

you have written the scroll view tag wrong. 您将滚动视图标签写错了。 That's the reason its not working. 这就是它不起作用的原因。 Change it from <Scrollview> to <ScrollView> and add the Scroll view end tag like </ScrollView> and re run the project, it will work fine. 将其从<Scrollview>更改为<ScrollView>并添加Scroll View end标记(如</ScrollView>并重新运行该项目,它将可以正常工作。

Hope this will help you ! 希望这个能对您有所帮助 !

1) You've misspelled the ScrollView tag it must be <ScrollView> not <Scrollview> 1)您拼写了ScrollView标记,它必须是<ScrollView>而不是<Scrollview>

2) You didn't close the <ScrollView> tag at the end of the code 2)您没有在代码末尾关闭<ScrollView>标记

-->Here's the right code ->这里是正确的代码

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:fillViewport="true"
 android:layout_height="match_parent">

<RelativeLayout
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#659D32"
  android:paddingBottom="16dp"
  android:paddingLeft="16dp"
  android:paddingRight="16dp"
  android:orientation="vertical"
  android:paddingTop="16dp"
  tools:context="com.example.MainActivity">

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardCornerRadius="5dp"
android:id="@+id/cardview_1"
app:cardElevation="10dp"
app:cardMaxElevation="15dp">

 <RelativeLayout
    android:layout_width="match_parent"

    android:layout_height="match_parent">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:src="@drawable/img_1"
        android:id="@+id/cardImage_bang"
        android:scaleType="centerCrop"/>

    <TextView
        android:layout_below="@+id/cardImage_1"
        android:id="@+id/cardTitle_1"
        android:gravity="center_horizontal"
        android:text="Text1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardCornerRadius="5dp"
android:layout_below="@+id/cardview_1"
android:id="@+id/cardview_2"
app:cardElevation="10dp"
android:layout_marginTop="10dp"

app:cardMaxElevation="15dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:src="@drawable/img_2"
        android:id="@+id/cardImage_2"
        android:scaleType="centerCrop"/>

    <TextView
        android:layout_below="@+id/cardImage_2"
        android:id="@+id/cardTitle_2"
        android:gravity="center_horizontal"
        android:text="Text2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM