2012-04-25 22 views
44

Tôi muốn gán trọng số bố cục cho một số mục trong một LinearLayout bên trong của một ScrollView. Tuy nhiên, số ScrollView bỏ qua LinearLayoutweightSum.Trọng lượng bố cục không hoạt động bên trong một ScrollView

Mục tiêu của tôi là chia bố cục với trọng số là 2, 1, 1 (với tổng số là 4), nhưng điều này không hoạt động chính xác bên trong số ScrollView.

Làm cách nào để giải quyết vấn đề bố cục này?

main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:weightSum="4"> 

     <LinearLayout android:id="@+id/logo" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_weight="2" 
      android:background="#FFFFFF" /> 

     <LinearLayout android:id="@+id/logo1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_weight="1" 
      android:background="#000000" /> 


     <LinearLayout android:id="@+id/logobutton" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="1" 
      android:background="#4B4B4B" /> 

    </LinearLayout> 
</ScrollView> 
+0

nếu tôi đang loại bỏ chỉ scrollview hơn là công việc nhưng câu hỏi của tôi là nếu sáng sử dụng scrollview hơn không được làm việc –

+0

việc kiểm tra này một: [Android Layout Trọng lượng] (http://stackoverflow.com/questions/4986861/ android-bố trí-trọng lượng) Tôi nghĩ rằng nó loại cùng một vấn đề. – user1264255

Trả lời

137

Tôi đã gặp vấn đề này trước đây. Chỉ cần sử dụng android:fillViewport="true" trong ScrollView của bạn và nó sẽ lấp đầy màn hình.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" > 
+1

@MIPTECH: thử câu trả lời này. Thông tin thêm tại http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/ – Akhil

+2

+1 Làm việc như quyến rũ..Cảm ơn !!! – Venky

+0

Cảm ơn người đàn ông, tôi đã thực sự tìm kiếm một giải pháp và đó là chính xác những gì tôi cần. –

2

này sẽ không làm việc như bạn đã làm nó. Chế độ xem con của ScrollView phải được đặt thành wrap_content. Nếu bạn đặt nó là fill_parent, nó sẽ lấp đầy diện tích của ScrollView và không bao giờ cuộn, bởi vì nó sẽ không lớn hơn ScrollView.

Ý tưởng về layout_weight là để lấp đầy một khu vực cụ thể tương ứng.

Bạn nên đặt tất cả các con LinearLayoutslayout_height cho một trong hai wrap_content hoặc một kích thước cụ thể (trong dp) và phụ huynh LinearLayoutlayout_height để wrap_content

Như đã nói bạn cần phải loại bỏ các thêm

xmlns : android = "http://schemas.android.com/apk/res/android"

nếu nó không phải là gốc (linh sam st) phần tử của bố cục.

Các vấn đề liên quan