2013-11-26 52 views
2

我无法在ScrollView下看到最多的视图。 我已将scrollview放置在相对布局下。 ScrollView有一个子布局,即具有一系列按钮的线性布局。 问题是我无法看到ScrollView中存在的最顶层视图。滚动视图不显示Android中的所有视图

下面是布局xml代码。

<?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:background="#217d27" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" > 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="10dp" 
      android:onClick="backPressed" 
      android:text="Back" 
      android:textColor="#000" /> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:gravity="center" 
      android:text="Heading Text" 
      android:textColor="#fff" 
      android:textSize="20sp" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="10dp" 
      android:onClick="homeButtonClicked" 
      android:text="Home" 
      android:textColor="#000" /> 
    </RelativeLayout> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:fadingEdge="none" 
     android:fillViewport="true" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="#fff" 
      android:orientation="vertical" > 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:text="Button 1" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 2" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 3" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 4" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 5" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 6" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 7" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 8" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 9" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 10" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 11" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 12" 
       android:textColor="@color/black" /> 
     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 

请参阅屏幕截图以供参考。

Screen shot

+0

使用相对布局而不是线性作为父项。 –

+0

Nope相对布局也不起作用。我得到了同样的结果。 – ravi

+0

你的代码是99%完美只是删除1行只看到我的回答我更新你的代码伙计和你总是欢迎:) –

回答

8

只是删除线性布局这条线是滚动视图的直接子 机器人:layout_gravity =“中心”试试这个代码更新

<?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:background="#217d27" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" > 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="10dp" 
      android:onClick="backPressed" 
      android:text="Back" 
      android:textColor="#000" /> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:gravity="center" 
      android:text="Heading Text" 
      android:textColor="#fff" 
      android:textSize="20sp" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="10dp" 
      android:onClick="homeButtonClicked" 
      android:text="Home" 
      android:textColor="#000" /> 
    </RelativeLayout> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:fadingEdge="none" 
     android:fillViewport="true" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="#fff" 
      android:orientation="vertical" > 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:text="Button 1" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 2" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 3" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 4" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 5" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 6" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 7" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 8" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 9" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 10" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 11" 
       android:textColor="@color/black" /> 

      <Button 
       android:layout_width="200dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="10dp" 
       android:text="Button 12" 
       android:textColor="@color/black" /> 
     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 
1

尝试将父布局更改为线性。它可能工作。不知道如何和为什么,但对我来说,它在类似的情况下工作。试一试。

7

只要改变LinearLayoutScrollView

<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top|center" 
     android:background="#fff" 
     android:orientation="vertical" > 

给出android:layout_gravity="top|center"而不是android:layout_gravity="center"

希望它有帮助。

+0

非常感谢你Anuj。 – ravi

+0

噢,我的天啊..太多了:D – Jenson

3

此值帮我解决了类似的问题:在滚动型 fillViewport =“true”,并使用Android: 集机器人重力,而不是机器人:在滚动型孩子layout_gravity,在我的情况下的LinearLayout清除所有重量。

-1

在LinearLayout中使用android:layout_gravity =“center”