簡體   English   中英

ScrollView中的LinearLayout自動滾動到底部

[英]LinearLayout inside ScrollView is autoscrolled to bottom

我有一個ScrollView,它占據了屏幕的下半部分。 在此ScrollView內放置了一個內容很多的LinearLayout(垂直)。 當我開始活動時,內容為什么會自動向下滾動,使其從窗口頂部開始。 但是我需要它從ScrollView的頂部開始(即在窗口的中心)。 我究竟做錯了什么? 在此處輸入圖片說明

<LinearLayout 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"
    tools:context="ru.intrigue.activities.EditActivity"
    android:orientation="vertical">

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:visibility="visible">

 </RelativeLayout>

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView2"
        android:background="@color/colorDarkBack">

           <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="10dp">


                 <!-- content -->

          </LinearLayout>
    </ScrollView>
</LinearLayout>

您可以執行以下操作:

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.45"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/frag_home_iv_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@drawable/demo" />

</LinearLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.55"
    android:fadeScrollbars="false">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.26"
            android:gravity="center"
            android:orientation="horizontal"
            android:padding="@dimen/padding_5dp"
            android:weightSum="1">

            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:src="@drawable/demo" />

            <TextView
                android:id="@+id/fragment_home_tv"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.7"
                android:paddingLeft="@dimen/padding_5dp"
                android:text="@string/demo"
                android:textColor="@color/BlackColor" />
        </LinearLayout>
</LinearLayout>


</ScrollView>
</LinearLayout>

您的問題通過以下代碼解決:

當打開您的活動調用時,代碼如下:

your_scroll_view.post(new Runnable() {
                        @Override
                        public void run() {

                            your_scroll_view.fullScroll(View.FOCUS_DOWN);
                        }
                    });

祝你好運

您可以使用layout_weight屬性獲得所需的結果:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <!-- your content here-->

    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

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

            <!-- your content here-->

        </LinearLayout>

    </ScrollView>

</LinearLayout>

讓我知道它是否對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM