简体   繁体   English

在一个XML文件中以线性和相对布局添加ScrollView

[英]Add a ScrollView in Linear and Relative Layout in one XML file

I am trying to make the contents of Relative Layout as Scrollview as its not fitting in the screen. 我试图使相对布局的内容作为Scrollview,因为它不适合屏幕。 My XML file that consists of both, Linear and Relative Layout where Linear is acting as Parent and Relative as child layout. 我的XML文件包含线性和相对布局,其中线性充当父布局,相对充当子布局。

Although I have browsed, most of the questions regarding this topic, but all i could find is how to insert Scrollview in Linear or Relative Layout respectively but not when both layouts are in one file. 尽管我已经浏览了有关该主题的大多数问题,但是我能找到的只是如何分别在线性或相对布局中插入Scrollview,而在两种布局都在一个文件中时却没有。

my XML file 我的XML档案

<?xml version="1.0" encoding="UTF-8"?>

<!-- 
   Being of Linear Layout
 -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/week1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/week1"
        android:textColor="#FFFFFF"
        android:textSize="25sp"
        android:paddingTop="50dp"
        android:background="#00141c" />
<!--
Begin Relative Layout
-->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:scrollHorizontally="true">

    <ImageView
        android:id="@+id/treadmillimage"
        android:layout_width="115dp"
        android:layout_height="93dp"
        android:paddingTop="10dp"
        android:src="@drawable/treadmill" />

    <EditText
        android:id="@+id/durOnTreadmill"
        android:layout_width="129dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:layout_marginLeft="180dp" 
        android:layout_centerVertical="true"/>

<!--
End Relative Layout
-->
    </RelativeLayout>

    <TextView
        android:id="@+id/treadmilltext"
        android:layout_width="91dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="@string/treadmill"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

<!--
Begin Relative Layout
-->

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:scrollHorizontally="true">

    <ImageView
        android:id="@+id/stepperimage"
        android:layout_width="115dp"
        android:layout_height="93dp"
        android:paddingTop="10dp"
        android:src="@drawable/stepper" />

    <EditText
        android:id="@+id/durOnStepper"
        android:layout_width="129dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:layout_marginLeft="180dp" 
        android:layout_centerVertical="true"/>

<!--
End Relative Layout
-->
    </RelativeLayout>

    <TextView
        android:id="@+id/steppertext"
        android:layout_width="91dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="@string/stepper"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

<!--
Begin Relative Layout
-->
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:scrollHorizontally="true">

    <ImageView
        android:id="@+id/stationaryrowing"
        android:layout_width="115dp"
        android:layout_height="93dp"
        android:paddingTop="10dp"
        android:src="@drawable/stationaryrowing" />

    <EditText
        android:id="@+id/durOnStationaryRowing"
        android:layout_width="129dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:layout_marginLeft="180dp" 
        android:layout_centerVertical="true"/>

<!--
End Relative Layout
-->
    </RelativeLayout>

    <TextView
        android:id="@+id/stationaryrowingtext"
        android:layout_width="91dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="@string/stationaryrowing"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

<!--
Begin Relative Layout
-->
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:scrollHorizontally="true">

    <ImageView
        android:id="@+id/exercisebikeimage"
        android:layout_width="115dp"
        android:layout_height="93dp"
        android:paddingTop="10dp"
        android:src="@drawable/excerisebike" />

    <EditText
        android:id="@+id/durOnexcerisebike"
        android:layout_width="129dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:layout_marginLeft="180dp" 
        android:layout_centerVertical="true"/>

 <!--
End Relative Layout
-->
    </RelativeLayout>

     <TextView
        android:id="@+id/excerisebiketext"
        android:layout_width="91dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="@string/exercisebike"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

 <!--
Begin Relative Layout
-->
       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:scrollHorizontally="true">

    <ImageView
        android:id="@+id/ellipticaltrainerimage"
        android:layout_width="115dp"
        android:layout_height="93dp"
        android:paddingTop="10dp"
        android:src="@drawable/ellipticaltrainer" />

    <EditText
        android:id="@+id/durOnellipticaltrainer"
        android:layout_width="129dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:layout_marginLeft="180dp" 
        android:layout_centerVertical="true"/>

<!--
End Relative Layout
-->
    </RelativeLayout>

        <TextView
        android:id="@+id/ellipticaltrainertext"
        android:layout_width="91dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:text="@string/ellipticaltrainer"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

<!--
End Linear Layout
-->
</LinearLayout>   

All the contents below TextView of Week 1 are required to be in Scrollview 第1周TextView下面的所有内容都必须在Scrollview中

Thanks in advance, will really appreciate all the answers :) 在此先感谢,将非常感谢所有答案:)

If you don't want the topmost TextView to be part of the scrolling area, then you'll have to nest the RelativeLayout s into another layout, because a ScrollView can have only one direct child. 如果您不希望最顶层的TextView成为滚动区域的一部分,则必须将RelativeLayout嵌套到另一个布局中,因为ScrollView只能有一个直接子级。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView ... />

    <ScrollView ... >
        <!-- only one direct child for scrollview: -->
        <LinearLayout ... >

            <RelativeLayout ... />
            <TextView ... />
            <RelativeLayout ... />
            <TextView ... />
            <RelativeLayout ... />
            <TextView ... />
            <RelativeLayout ... />
            <TextView ... />
            <!-- etc -->

        </LinearLayout>

    </ScrollView>

</LinearLayout>

just put the scorolleview as parent of the RelativeLayout as follows: 只需将scorolleview放置为RelativeLayout的父级,如下所示:

<ScrollView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:scrollHorizontally="true">

    <ImageView
        android:id="@+id/treadmillimage"
        android:layout_width="115dp"
        android:layout_height="93dp"
        android:paddingTop="10dp"
        android:src="@drawable/treadmill" />

.....
.....
</RelativeLayout>
</ScrollView>

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

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