2016-07-28 72 views
1

我有一個ScrollView佔據了屏幕的下半部分。 在這個ScrollView裏放置了一個LinearLayout(垂直),內容很多。 當我開始活動時,內容會自動向下滾動,以便從窗口頂部開始。但我需要它從ScrollView的頂部開始(即在窗口的中心)。 我在做什麼錯? enter image description hereScrollView內的LinearLayout被自動滾動到底部

<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> 
+0

張貼相關的代碼以及 – SMR

+0

你如何設置你的'滾動型'佔據屏幕的一半? – SlashG

+0

把你的xml放在這裏 –

回答

0

你可以做象下面這樣:

<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> 
0

您可以通過使用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> 

讓我知道,如果它幫你。

+0

恐怕沒有。我確實喜歡你說過,但仍然是同樣的行爲。 –

+0

[編輯](http://stackoverflow.com/posts/38627466/edit)你的答案與更新的代碼 – SMR

1

您的問題,下面的代碼解決了:

當打開下面的代碼您的活動電話:

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

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

最好的運氣