2010-08-05 57 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

相關問題