How to make whole activity scrollable according to the data inside it

帅比萌擦擦* 提交于 2019-12-21 17:33:01

问题


I have tried LinearLayout inside which I put several textview. I completely use weights for each and every view. Problem arises when data becomes too much in lenght and textview not shows it completely. I also tried tabletlayout but that thing also not works beacuse ultimately tablelayout is inside linear layout having weight,so it doesn't grows beyond a certain limit. I want to know how can I make a activity scrollable with showing all data while using weights.


回答1:


Use Scrollview in xml Layout of your activity.

Example: XML code for your activity:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"
    android:fillViewport="false">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@drawable/image" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="KNOW MORE" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/description"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>
</ScrollView>



回答2:


You can Use Scroll View With Linear Layout as well as With Relative layout.

You Can try this code, Just Copy and paste in XML File. U'll Get Your ans :)

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

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@android:color/black"
>

<TextView
    android:id="@+id/TV_Welcome"
    android:text="ADD YOUR VEHICLE"
    android:layout_marginLeft="70dp"
    android:layout_marginTop="10dp"
    android:textSize="20dp"
    android:layout_centerHorizontal="true"

    android:textColor="@color/menu_color"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical|center_horizontal"/>


<RelativeLayout
    android:id="@+id/RelativeLayout01"
    android:layout_below="@+id/TV_Welcome"
    android:layout_margin="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">


    <TextView
        android:id="@+id/TV_BlockNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Block No:"
        android:textColor="@color/version_color"
        android:layout_marginLeft="10dp"
        android:textSize="15dp"
        android:layout_alignParentLeft="true"
        android:gravity="left"

        />

    <Spinner
        android:id="@+id/Spinner_BlockNo"
        android:layout_marginLeft="30dp"
        android:layout_toRightOf="@+id/TV_BlockNo"
        android:background="#558cff"
        android:layout_width="300dp"
        android:layout_height="wrap_content">

    </Spinner>
</RelativeLayout>

<RelativeLayout
    android:id="@+id/RelativeLayout02"
    android:layout_margin="10dp"
    android:layout_below="@+id/RelativeLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/TV_FlatNo"
        android:text="Flat NO:"
        android:textSize="15dp"
        android:layout_marginLeft="10dp"
        android:textColor="@color/version_color"

        />

    <Spinner
        android:id="@+id/Spinner_FlatNo"
        android:layout_marginLeft="42dp"
        android:layout_toRightOf="@+id/TV_FlatNo"
        android:background="#558cff"
        android:layout_width="300dp"

        android:layout_height="wrap_content"></Spinner>

</RelativeLayout>

<RelativeLayout
    android:id="@+id/RelativeLayout03"
    android:layout_below="@+id/RelativeLayout02"
    android:layout_margin="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">


    <TextView
        android:id="@+id/TV_VehicleNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Vehicle No:"
        android:textColor="@color/version_color"
        android:layout_marginLeft="10dp"
        android:textSize="15dp"
        android:layout_alignParentLeft="true"
        android:gravity="left"

        />

    <EditText
        android:id="@+id/ET_VehicleNo"
        android:layout_marginLeft="20dp"
        android:hint="Enter Vehicle No"
        android:textColorHint="@color/menu_glow"
        android:textSize="20dp"
        android:layout_toRightOf="@+id/TV_VehicleNo"
        android:textColor="#558cff"
        android:layout_width="400dp"
        android:layout_height="wrap_content"

        />


</RelativeLayout>


<RelativeLayout
    android:id="@+id/RelativeLayout04"
    android:layout_margin="10dp"
    android:layout_below="@+id/RelativeLayout03"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/TV_VehicleType"
        android:text="Vehicle Type:"
        android:textSize="15dp"
        android:layout_marginLeft="10dp"
        android:textColor="@color/version_color"

        />

    <Spinner
        android:id="@+id/Spinner_VehicleType"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/TV_VehicleType"
        android:background="#558cff"
        android:layout_width="300dp"
        android:layout_height="wrap_content">

    </Spinner>

</RelativeLayout>

<RelativeLayout
    android:id="@+id/RelativeLayout05"
    android:layout_margin="10dp"
    android:layout_below="@id/RelativeLayout04"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/TV_OwnerName"
        android:text="Owner Name:"
        android:textSize="15dp"
        android:textColor="@color/version_color"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentLeft="true"
        android:gravity="left">
    </TextView>
    <EditText
        android:id="@+id/ET_OwnerName"
        android:hint="Enter Owner Name"
        android:textColorHint="@color/menu_glow"
        android:inputType="textPersonName"
        android:layout_marginLeft="20dp"
        android:textSize="20dp"
        android:layout_toRightOf="@id/TV_OwnerName"
        android:textColor="@color/version_color"
        android:layout_height="wrap_content"
        android:layout_width="400dp">
    </EditText>
</RelativeLayout>


<RelativeLayout
    android:id="@+id/RelativeLayout06"
    android:layout_margin="10dp"
    android:layout_below="@id/RelativeLayout05"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/TV_MobileNo"
        android:text="Mobile No:"
        android:textSize="15dp"
        android:textColor="@color/version_color"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentLeft="true"
        android:gravity="left">
    </TextView>
    <EditText
        android:id="@+id/ET_MobileNo"
        android:layout_marginLeft="40dp"
        android:hint="Enter 10 digit"
        android:textColorHint="@color/menu_glow"
        android:inputType="phone"
        android:textSize="20dp"
        android:layout_toRightOf="@id/TV_MobileNo"
        android:textColor="@color/version_color"
        android:layout_height="wrap_content"
        android:layout_width="200dp"

        >
    </EditText>
</RelativeLayout>


<RelativeLayout
    android:id="@+id/RelativeLayout07"
    android:layout_margin="10dp"
    android:layout_below="@id/RelativeLayout06"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/TV_TelNo"
        android:text="Telephone No:"
        android:textSize="15dp"
        android:textColor="@color/version_color"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
    </TextView>
    <EditText
        android:id="@+id/ET_TelNo"
        android:layout_marginLeft="20dp"
        android:hint="Enter Telephone No"
        android:textColorHint="@color/menu_glow"
        android:inputType="number"
        android:textSize="17dp"
        android:layout_toRightOf="@id/TV_TelNo"
        android:textColor="@color/version_color"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
    </EditText>
</RelativeLayout>


<RelativeLayout
    android:id="@+id/RelativeLayout08"
    android:layout_margin="0dp"
    android:layout_below="@id/RelativeLayout07"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/TV_Example"
        android:text="(Example:0141-2206923)"
        android:layout_marginLeft="120dp"
        android:textSize="10dp"
        android:textColor="@color/version_color"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
    </TextView>
</RelativeLayout>

<ImageView
    android:id="@+id/ImageView_Submit"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="100dp"
    android:layout_centerInParent="true"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:src="@drawable/submit"
    android:layout_below="@id/RelativeLayout08"
    android:onClick="doSubmit"></ImageView>

    </RelativeLayout>
</ScrollView>



回答3:


You can make your activity scrollable using ScrollView. Its very simple and effective to use.Just copy code of ScrollView from below and paste it in your layout xml file. You can use this ScrollView with Linear as well as Relative Layout also. Just need to remember one thing,ScrollView can have only one child widget.If you want more Children then wrap them into one container.

<?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="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

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

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/xyz.png"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView1"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView2"/>

            <TextView
                android:id="@+id/textView3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView3"/>

            <TextView
                android:id="@+id/textView4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView4"/>

        </LinearLayout>

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

            <Button
                android:id="@+id/btn_demo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Demo" />

        </LinearLayout>

    </LinearLayout>

</ScrollView>



回答4:


Basically you have to put your entire xml layout inside a scroll view. See the other code examples.



来源:https://stackoverflow.com/questions/33582546/how-to-make-whole-activity-scrollable-according-to-the-data-inside-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!