2015-11-17 39 views
0

我與ListView的問題是隻有ListView是可滾動的而不是整個屏幕。填充LinearLayout?

舉例來說,如果我有類似下面的佈局,只有屏幕的ListView的一部分是滾動的,而我希望整個屏幕滾動:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/toolbar" 
    android:paddingLeft="24dp" 
    android:paddingRight="24dp" 
    android:paddingTop="24dp" 
    android:paddingBottom="24dp" 
    android:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Hello, World!" 
      /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Lorem ipsum." 
      /> 

     <ListView 
      android:id="@+id/listview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      /> 

</LinearLayout> 

是否有可能來填充LinearLayout ,像上面那樣,爲了使整個屏幕可以滾動?

+0

在這種情況下,你可能不希望ListView。只需添加listview的內容 - 我不知道你在那裏有什麼,也許textviews或所以 - 動態到你的linearlayout應該在scrollview內。 – poss

回答

1

有處理這種更簡單的方法,ListView控件提供了addHeaderView(View)方法,所以你可以這樣做:

創建header_layout.xml:

<?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:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Hello, World!"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Lorem ipsum."/> 
    </LinearLayout> 

然後讓main_activity.xml

 <?xml version="1.0" encoding="utf-8"?> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/toolbar" 
     android:paddingLeft="24dp" 
     android:paddingRight="24dp" 
     android:paddingTop="24dp" 
     android:paddingBottom="24dp"> 
     <ListView 
      android:id="@+id/listview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </FrameLayout> 

然後在您的MainActivity.java中

 public class MainActivity extends Activity{ 

      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main_activity); 

       // reference your list 
       ListView listView = (ListView) findViewById(R.id.listview); 
       // Inflate the header and add the view 
       View headerView = LayoutInflater.from(MainActivity.this).inflate(R.layout.header_view, null); 
       listView.addHeaderView(headerView); 

      } 
    } 

參見http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)

1

是的,這是可能的。 簡單的方法 - 包裝一切ScrollView其醜陋,不建議,但可以工作。

更好的方法 - ListViewaddHeaderView(View v)addFooterView(View v)如果你想擁有的東西上面使用的頭,如果你想在下面的東西只是使用頁腳。在你的情況下,把這兩個TextViews到新的佈局文件。像這裏一樣膨脹它

LayoutInflater.from(context).inflate(R.layout.new_created_layout_with_these_two_text_views,null) 

並將創建的視圖添加爲標題。

0

有什麼你正在嘗試做的

1)您不能滾動LinearLayout兩個問題。爲了滾動LinearLayout,你必須插入ScrollView作爲其第一個和唯一的孩子,然後把你正在嘗試做是行不通的ScrollView

2裏面所有的子視圖)。 ScrollViewListView只是進入內容的窗口。實際的ListView本身只與它在屏幕上佔用的空間一樣大,而不是它所包含內容的大小。如果您希望ListViewTextView的內容一起滾動,您應該將TextView的內容添加到ListView中,或者將包裝視圖添加爲ListView的標題視圖。您的其他選擇是根本不使用ListView,只需將所有的孩子以編程方式附加到您的ScrollView

注意:您不應該使用ScrollView AND a ListView。選擇一種方法並堅持下去。