2010-08-05 77 views
139

在我开始活动后,我无法向下滚动查看下面定义的xml中的其他按钮和选项。如何使LinearLayout可滚动

有谁知道如何使这个滚动?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:background="#000044" 
android:isScrollContainer="true" 
android:orientation="vertical"> 
<TextView 
    android:id="@+id/title" 
    android:text="@string/title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#ffffff"/> 
<EditText 
    android:id="@+id/editTitle" 
    android:text="" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
<TextView 
    android:id="@+id/description" 
    android:text="@string/description" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#ffffff"/> 
<EditText 
    android:id="@+id/editDescription" 
    android:text="" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
<TextView 
    android:id="@+id/location" 
    android:text="@string/location" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#ffffff"/> 
<EditText 
    android:id="@+id/editLocation" 
    android:text="" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
<TextView 
    android:id="@+id/startTime" 
    android:text="@string/startTime" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#ffffff"/> 
<DatePicker 
    android:id="@+id/DatePicker01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
<TimePicker 
    android:id="@+id/TimePicker01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
<TextView 
    android:id="@+id/endTime" 
    android:text="@string/endTime" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#ffffff"/> 
<DatePicker 
    android:id="@+id/DatePicker02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
<TimePicker 
    android:id="@+id/TimePicker02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
<Button 
    android:id="@+id/buttonCreate" 
    android:text="Create" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
</LinearLayout> 

我的类代码为

package com.example.blah; 

import android.app.Activity; 
import android.os.Bundle; 

public class example extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

输出是http://img265.imageshack.us/img265/7601/linearlayout.jpg

+0

[你如何制作一个LinearLayout可滚动?](http://stackoverflow.com/questions/4055537/how-do-you-make-a-linearlayout-scrollable) – Flow 2016-08-27 10:01:58

回答

204

将您在ScrollView布局。

+0

完美答案! – Sana 2010-08-05 16:22:08

+6

这是副作用,例如不能将滚动元素放置在滚动视图中,如列表视图。 – JoaoFilipeClementeMartins 2014-01-23 11:56:40

+2

您应该避免可滚动内容中的可滚动内容。用户不喜欢它(你永远不知道你会移动哪个卷轴)。 – Guillaume 2014-01-23 14:34:31

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

<LinearLayout ...> 

... 
... 

</LinearLayout> 

</ScrollView> 
15

将您的整个内容置于放置在ScrollView内部的线性布局。

ScrollView只有一个布局作为其子。

isScrollContainer="true" 

此属性用于在您的android软键弹出时仍然希望视图滚动。

2

您也可以使用View.scrollTo(..)来实现它。

postDelayed(new Runnable() { 
      public void run() { 

       counter = (int) (counter + 10); 
       handler.postDelayed(this, 100); 

        llParent.scrollTo(counter , 0); 
       } 

      } 
     }, 1000L); 
54
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center|left" 
     android:orientation="vertical"> 

     Your views go here... 

    </LinearLayout> 

</ScrollView> 
2

,你可以做任何布局滚动。刚下<?xml version="1.0" encoding="utf-8"?>加上这些行:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

,并在末尾添加非滚动活动</ScrollView>

例如:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:verticalScrollbarPosition="right" 
    tools:context="p32929.demo.MainActivity"> 


    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="102dp" 
     android:id="@+id/textView" 
     android:textSize="30sp" /> 
</RelativeLayout> 

使其滚动后,就变成这样:

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:verticalScrollbarPosition="right" 
     tools:context="p32929.demo.MainActivity"> 


     <TextView 
      android:id="@+id/textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="102dp" 
      android:text="TextView" 
      android:textSize="30sp" /> 
    </RelativeLayout> 
</ScrollView> 
1

将所有布局放置在宽度为h的ScrollView中八个设置为fill_parent。

+0

您的回复看起来像是评论而不是答案。一旦你有足够的[声誉](http://stackoverflow.com/help/whats-reputation),你将可以在任何帖子上[评论](http://stackoverflow.com/help/privileges/comment)。同时检查这个[我可以做什么,而不是](https://meta.stackexchange。COM /问题/ 214173 /为什么-DO-I-需要50声誉对评论 - 什么 - 可以-I-DO-代替)。如果您打算给出答案,请阅读[如何回答](http://stackoverflow.com/help/how-to-answer)以提供高质量的答案。 – thewaywewere 2017-05-03 13:51:09

+0

fill_parent已经被弃用,并替换为match_parent – 2018-01-03 16:07:01

相关问题