Android滚动视图位于APP栏顶部

时间:2016-03-23 09:20:22

标签: android xml scrollbar

您好我尝试将滚动添加到我的活动中。

我的活动有两个文件

1)activity_temple_details

2)content_temple_details

当我尝试滚动时内容在应用栏顶部可见。

像这样

enter image description here

我想在滚动

时将内容放在appbar或disapper后面

这是我的代码

activity_temple_details

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



    <android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
        tools:context="baman.lankahomes.lk.jaffnatemples.Temple_Details">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>



        <include layout="@layout/content_temple__details" />



    </android.support.design.widget.CoordinatorLayout>

content_temple_details

    <?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">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:layout_centerInParent="true"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="baman.lankahomes.lk.jaffnatemples.Temple_Details"
        tools:showIn="@layout/activity_temple__details">





        <ImageView
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/IV_temple_logo"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:src="@drawable/ic_action_movie"
            android:background="@drawable/image_view_style"
            android:layout_marginRight="5dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Temple Name"
            android:id="@+id/textView"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/IV_temple_logo"
            android:layout_toEndOf="@+id/IV_temple_logo"
            android:layout_marginLeft="8dp"
            android:textSize="14dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/TV_title"
            android:layout_below="@+id/textView"
            android:layout_toRightOf="@+id/IV_temple_logo"
            android:layout_toEndOf="@+id/IV_temple_logo"
            android:textSize="14dp"
            android:layout_marginLeft="8dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Address"
            android:id="@+id/textView3"
            android:layout_below="@+id/TV_title"
            android:layout_toRightOf="@+id/IV_temple_logo"
            android:layout_toEndOf="@+id/IV_temple_logo"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="8dp"
            android:textSize="14dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/TV_address"
            android:layout_below="@+id/textView3"
            android:layout_toRightOf="@+id/IV_temple_logo"
            android:layout_toEndOf="@+id/IV_temple_logo"
            android:textSize="14dp"
            android:layout_marginLeft="8dp" />






        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Description"
            android:id="@+id/textView5"
            android:layout_below="@+id/IV_temple_logo"
            android:layout_alignRight="@+id/IV_temple_logo"
            android:layout_alignEnd="@+id/IV_temple_logo"
            android:layout_marginTop="10dp"
            android:textSize="14dp" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/TV_Description"
            android:layout_below="@+id/textView5"
            android:layout_alignRight="@+id/TV_title"
            android:layout_alignEnd="@+id/TV_title"
            android:background="@drawable/textview_style"
            android:textSize="14dp"
            android:layout_marginTop="8dp"
            android:scrollbars = "vertical"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Directions"
            android:id="@+id/button"
            android:background="@drawable/button_selector"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="18dp"
            android:layout_marginTop="70dp"
            android:layout_below="@+id/TV_Description"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search Again"
            android:id="@+id/button2"
            android:background="@drawable/button_selector"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="18dp"
            android:layout_alignTop="@+id/button"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Suggest an Edit"
            android:id="@+id/button3"
            android:background="@drawable/button_selector"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="18dp"
            android:layout_below="@+id/button"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="15dp" />




    </RelativeLayout>
    </ScrollView>

可以帮助我解决这个问题。 TNX。

4 个答案:

答案 0 :(得分:1)

尝试在scrollview中添加以下行

android:layout_marginTop="?attr/actionBarSize"

我想这会有所帮助,它解决了我的问题。

布局文件

    <?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:layout_marginTop="?attr/actionBarSize"

>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:layout_centerInParent="true"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="baman.lankahomes.lk.jaffnatemples.Temple_Details"
        tools:showIn="@layout/activity_temple__details">





        <ImageView
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/IV_temple_logo"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:src="@drawable/ic_action_movie"
            android:background="@drawable/image_view_style"
            android:layout_marginRight="5dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Temple Name"
            android:id="@+id/textView"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/IV_temple_logo"
            android:layout_toEndOf="@+id/IV_temple_logo"
            android:layout_marginLeft="8dp"
            android:textSize="14dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/TV_title"
            android:layout_below="@+id/textView"
            android:layout_toRightOf="@+id/IV_temple_logo"
            android:layout_toEndOf="@+id/IV_temple_logo"
            android:textSize="14dp"
            android:layout_marginLeft="8dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Address"
            android:id="@+id/textView3"
            android:layout_below="@+id/TV_title"
            android:layout_toRightOf="@+id/IV_temple_logo"
            android:layout_toEndOf="@+id/IV_temple_logo"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="8dp"
            android:textSize="14dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/TV_address"
            android:layout_below="@+id/textView3"
            android:layout_toRightOf="@+id/IV_temple_logo"
            android:layout_toEndOf="@+id/IV_temple_logo"
            android:textSize="14dp"
            android:layout_marginLeft="8dp" />






        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Description"
            android:id="@+id/textView5"
            android:layout_below="@+id/IV_temple_logo"
            android:layout_alignRight="@+id/IV_temple_logo"
            android:layout_alignEnd="@+id/IV_temple_logo"
            android:layout_marginTop="10dp"
            android:textSize="14dp" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/TV_Description"
            android:layout_below="@+id/textView5"
            android:layout_alignRight="@+id/TV_title"
            android:layout_alignEnd="@+id/TV_title"
            android:background="@drawable/textview_style"
            android:textSize="14dp"
            android:layout_marginTop="8dp"
            android:scrollbars = "vertical"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Directions"
            android:id="@+id/button"
            android:background="@drawable/button_selector"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="18dp"
            android:layout_marginTop="70dp"
            android:layout_below="@+id/TV_Description"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search Again"
            android:id="@+id/button2"
            android:background="@drawable/button_selector"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="18dp"
            android:layout_alignTop="@+id/button"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Suggest an Edit"
            android:id="@+id/button3"
            android:background="@drawable/button_selector"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:textSize="18dp"
            android:layout_below="@+id/button"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="15dp" />




    </RelativeLayout>
    </ScrollView>

答案 1 :(得分:0)

/ *根据您的要求更改** /

<RelativeLayout
    android:id="@+id/relative_activity_main"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:orientation="vertical"
    android:background="@color/mainBackGroundColor"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:id="@+id/linearlayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <include
            android:id="@+id/app_bar_id"
            layout="@layout/app_bar"></include>
    <include
            android:id="@+id/yourlayout_id"
            layout="@layout/yourLayout"></include>
</LinearLayout>
</RelativeLayout>

答案 2 :(得分:0)

在CoordinatorLayout中你必须使用NestedScrollView而不是ScrollView, 这是你的解决方案

<强> content_temple_details.xml

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

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:layout_centerInParent="true"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="baman.lankahomes.lk.jaffnatemples.Temple_Details"
    tools:showIn="@layout/activity_temple__details">

    <ImageView
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:id="@+id/IV_temple_logo"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:src="@drawable/ic_action_movie"
        android:background="@drawable/image_view_style"
        android:layout_marginRight="5dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Temple Name"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/IV_temple_logo"
        android:layout_toEndOf="@+id/IV_temple_logo"
        android:layout_marginLeft="8dp"
        android:textSize="14dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/TV_title"
        android:layout_below="@+id/textView"
        android:layout_toRightOf="@+id/IV_temple_logo"
        android:layout_toEndOf="@+id/IV_temple_logo"
        android:textSize="14dp"
        android:layout_marginLeft="8dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Address"
        android:id="@+id/textView3"
        android:layout_below="@+id/TV_title"
        android:layout_toRightOf="@+id/IV_temple_logo"
        android:layout_toEndOf="@+id/IV_temple_logo"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="8dp"
        android:textSize="14dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/TV_address"
        android:layout_below="@+id/textView3"
        android:layout_toRightOf="@+id/IV_temple_logo"
        android:layout_toEndOf="@+id/IV_temple_logo"
        android:textSize="14dp"
        android:layout_marginLeft="8dp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Description"
        android:id="@+id/textView5"
        android:layout_below="@+id/IV_temple_logo"
        android:layout_alignRight="@+id/IV_temple_logo"
        android:layout_alignEnd="@+id/IV_temple_logo"
        android:layout_marginTop="10dp"
        android:textSize="14dp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/TV_Description"
        android:layout_below="@+id/textView5"
        android:layout_alignRight="@+id/TV_title"
        android:layout_alignEnd="@+id/TV_title"
        android:background="@drawable/textview_style"
        android:textSize="14dp"
        android:layout_marginTop="8dp"
        android:scrollbars = "vertical"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Directions"
        android:id="@+id/button"
        android:background="@drawable/button_selector"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="18dp"
        android:layout_marginTop="70dp"
        android:layout_below="@+id/TV_Description"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Search Again"
        android:id="@+id/button2"
        android:background="@drawable/button_selector"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="18dp"
        android:layout_alignTop="@+id/button"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Suggest an Edit"
        android:id="@+id/button3"
        android:background="@drawable/button_selector"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:textSize="18dp"
        android:layout_below="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="15dp" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>

答案 3 :(得分:0)

请在滚动视图中设置布局行为

        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"