2016-07-28 91 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); 
         } 
        }); 

最好的运气

相关问题