2013-08-25 36 views
18

Trong android, làm cách nào bạn có thể tạo chế độ xem cuộn có chiều cao tối đa và nội dung bọc, về cơ bản nó bao bọc nội dung theo chiều dọc, nhưng có chiều cao tối đa?Làm thế nào để thiết lập chiều cao tối đa với nội dung bọc trong android?

tôi đã cố gắng

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
     android:maxHeight="200dp" 
    android:layout_alignParentBottom="true" > 

    <LinearLayout 
     android:id="@+id/maincontainer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

    </LinearLayout> 
</ScrollView> 

Nhưng điều này không hoạt động?

+0

đây là một giải pháp khác cho vấn đề: https://stackoverflow.com/questions/42694355/how-to-set-recyclerview-max-height/48970129#48970129 – user1506104

Trả lời

-4

Ở đây bạn có thể thiết lập chiều cao của scrollview của bạn như thế này -:

<ScrollView 
android:id="@+id/scrollView1" 
android:layout_width="match_parent" 
android:layout_height="200dp" 
android:layout_alignParentBottom="true" > 

      <LinearLayout 
      android:id="@+id/maincontainer" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      </LinearLayout> 
</ScrollView> 
+0

Chắc chắn không trả lời câu hỏi! – Maverick283

16

tôi đã mở rộng scrollview và thêm mã để thực hiện tính năng này:

https://gist.github.com/JMPergar/439aaa3249fa184c7c0c

Tôi hy vọng rằng có ích .

+1

Triển khai này hoạt động cùng một lúc. Điều duy nhất tôi sẽ thêm là tham số maxHeight trong tệp/res/values ​​/ attrs.xml để có thể chỉ định nó ngay trong XML (http://developer.android.com/training/custom-views/create -view.html # customattr) – DNax

11

Bạn có thể làm điều đó theo chương trình.

private static class OnViewGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener { 
    private final static int maxHeight = 130; 
    private View view; 

    public OnViewGlobalLayoutListener(View view) { 
     this.view = view; 
    } 

    @Override 
    public void onGlobalLayout() { 
     if (view.getHeight() > maxHeight) 
      view.getLayoutParams().height = maxHeight; 
    } 
} 

Và thêm người nghe đến quan điểm:

view.getViewTreeObserver() 
        .addOnGlobalLayoutListener(new OnViewGlobalLayoutListener(view)); 

Listener sẽ gọi phương thức onGlobalLayout(), khi xem chiều cao sẽ được thay đổi.

+0

Tôi nghĩ rằng bạn cần phải loại bỏ 'void' trên kiến ​​trúc' OnViewGlobalLayoutListener (View view) '. – Alex

20

bạn có thể thêm video này vào bất kỳ quan điểm (ghi đè onMeasure trong một lớp kế thừa từ một cái nhìn)

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    if (maxHeight > 0){ 
     int hSize = MeasureSpec.getSize(heightMeasureSpec); 
     int hMode = MeasureSpec.getMode(heightMeasureSpec); 

     switch (hMode){ 
      case MeasureSpec.AT_MOST: 
       heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, maxHeight), MeasureSpec.AT_MOST); 
       break; 
      case MeasureSpec.UNSPECIFIED: 
       heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST); 
       break; 
      case MeasureSpec.EXACTLY: 
       heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(hSize, maxHeight), MeasureSpec.EXACTLY); 
       break; 
     } 
    } 

    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 
+0

Hoạt động. Như. A. Charm. –

+0

Cảm ơn bạn! nó hoạt động – Leon

+0

Nếu hoạt động hoàn hảo. Cảm ơn bạn rất nhiều! – Duyhungws

0

cho chiều cao bộ scrollview bạn phải sử dụng 2 linerlayout bên nhau và sau đó thiết lập xem scrool như chúng con sau đó thiết lập bố trí giữa lớp lót: chiều cao cho giới hạn chiều cao cuộn.