Sergey Metlov
Sergey Metlov

Reputation: 26291

Scrollable layout

How can I make such layout? Header should not scrolls and should be always visible.

enter image description here

The matter is Android throws an error when I try to put ScrollView into LinearLayout.

Markup:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              style="@style/baseLayout">
    <LinearLayout
        style="@style/app_header" />
    <ScrollView
        style="@style/fillParent">
        <!-- elements here -->
    </ScrollView>
</LinearLayout>

Styles:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="app_theme" parent="android:Theme">
    </style>

    <style name="fillParent">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
    </style>
    <style name="fullWidth">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>
    <style name="wrapContent">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="baseLayout" parent="fillParent">
        <item name="android:orientation">vertical</item>
        <item name="android:background">@drawable/app_bg</item>
    </style>

    <style name="app_header" parent="fullWidth">
        <item name="android:background">@drawable/header_bg</item>
    </style>
</resources>

Upvotes: 0

Views: 312

Answers (3)

prolink007
prolink007

Reputation: 34534

The following code works for me, could you post your code and let us see what you might be doing wrong?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TITLE BAR"/>
    <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        </LinearLayout>
    </ScrollView>
</LinearLayout>

Solution

The problem was that Ninja had more than 1 direct child in the ScrollView.

Upvotes: 2

Vyacheslav Shylkin
Vyacheslav Shylkin

Reputation: 9791

In your markup:

 ....  
 <ScrollView
      style="@style/fillParent">
       <!-- elements here -->
 </ScrollView>
 ... 

"elements here" - If more than one then you will get an error, because ScrollView can host only one direct child (for example layout).

Upvotes: 1

R.daneel.olivaw
R.daneel.olivaw

Reputation: 2679

It's not normal if there is an error if you put a Linear layout in side a Scroll view. Please try the below snippet in your code.

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

<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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TITLE BAR"/>

  <ScrollView 
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:orientation="vertical" >

      <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        .
        .
      </LinearLayout>

  </ScrollView>

</LinearLayout>

I hope this helps.

Upvotes: 1

Related Questions