15

Tôi đã tạo bố cục bằng cách sử dụng ScrollView có con số là PercentRelativeLayout. Nó không hoạt động trên Lollipop và các thiết bị cũ nhưng hoạt động tốt trên các thiết bị Marshmallow. Vui lòng kiểm tra mã bên dưới:PercentRelativeLayout bên trong ScrollView

<ScrollView 
    android:id="@+id/scrollView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

    <android.support.percent.PercentRelativeLayout 
     android:id="@+id/scrollContent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_heightPercent="100%" 
     app:layout_widthPercent="50%"> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@android:color/holo_red_dark" 
      android:text="kkjknadko" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textView1" 
      android:text="Abcaad" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview2" 
      android:background="@android:color/holo_red_dark" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview3" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview4" 
      android:background="@android:color/holo_red_dark" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview5" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview7" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview6" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview8" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview7" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

    </android.support.percent.PercentRelativeLayout> 
</ScrollView> 

Và tôi cũng không hiển thị bất kỳ điều gì trong các phiên bản Android cũ hơn và Lollipop.

Thật không may, bố cục phần trăm sẽ không hoạt động với ScrollView trước M. Lý do là vì chúng phụ thuộc vào gợi ý kích thước được phân phối trong bước đo. Trước khi M bố cục nhiều nhất sẽ cung cấp gợi ý kích thước 0 khi gửi thông số đo không xác định.

Bạn có thể cố gắng để khắc phục điều đó bằng cách tạo lớp con của riêng bạn ScrollView và trọng measureChildmeasureChildWithMargins (may mắn thay cả hai đều được bảo vệ) để cung cấp các gợi ý kích thước.

source - plus.google.com.

Ai đó có thể giúp tôi tạo tùy chỉnh ScrollView để làm cho nó hoạt động không?

+0

Cố gắng sử dụng NestScrollView thay vì scrollview. –

+0

tôi đã thử nhưng nó không hoạt động –

Trả lời

0

Câu hỏi như tôi hiểu là: Tôi muốn các TextView này có chiều rộng là 50% chế độ xem gốc.

Way mà làm việc đối với tôi là:

<Space 
    android:layout_width="20dp" 
    android:layout_height="20dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/space" /> 

Sau đó allign View (s) để đó. Hoặc là RelativeLayout hoặc TextViews. Bạn mất quyền kiểm soát (nếu bạn đã từng có nó) so với tỷ lệ phần trăm của RelativeLayout. Tôi có xu hướng đi với những thứ như:

android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 

Điều này có hữu ích không?

+0

Không, thực ra tôi phải làm phần trămRelativeLayout để làm việc bên trong scrollview. Tôi chỉ đưa ra ví dụ trên để hiển thị kịch bản không giống chính xác mà tôi đang sử dụng –

7

Tạo một tùy chỉnh scrollview và thiết lập Measured HeightWidth như bạn muốn nó cho Ví dụ

CustomScrollView

public class CustomScrollView extends ScrollView { 


    public CustomScrollView(Context context) { 
     super(context); 
    } 

    public CustomScrollView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     int size = 0; 
     int width = getMeasuredWidth(); 
     int height = getMeasuredHeight(); 

     if (width > height) { 
      size = height; 
     } else { 
      size = width; 
     } 
     setMeasuredDimension(size, size); 
    } 
} 

Sử dụng customscrollview trong bạn xml.

<yourscrollviewclasspath.CustomScrollView   // here you have scrollview path like com.yourpackage.folder_where_your_scrollview_lies 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true" 
     > 
</yourscrollviewclasspath.CustomScrollView > 

enter image description here hy vọng điều này sẽ hữu ích.

0

tôi gặp phải vấn đề tương tự với android K và android L.

Bạn có thể đã sử dụng bố trí hạn chế thay vì phần trăm bố trí tương đối nhưng có vẻ di chuyển vấn đề với điều đó quá. Ngay cả tùy chỉnh Scroll View như đề xuất trong câu trả lời khác không hữu ích cho tôi.

Mặc dù có một cách.

Chuyển đổi bố cục tương đối phần trăm thành bố cục tương đối và đặt chiều cao của chế độ xem theo chương trình theo chiều cao thiết bị của bạn.

Dường như không có vấn đề gì khi cuộn với bố cục tương đối. Và bạn có thể sử dụng chế độ xem 'Không gian' cho lề giữa TextViews. Đây không phải là một giải pháp rất thanh lịch nhưng đối với tôi nó đã làm việc.

Java Code:

int height = getWindowManager().getDefaultDisplay().getHeight(); 
int width = getWindowManager().getDefaultDisplay().getWidth(); 

// to set height to each textview to 10% of screen 
textView1.getLayoutParams().height = (int) (height * 0.10); 
textView2.getLayoutParams().height = (int) (height * 0.10); 
textView3.getLayoutParams().height = (int) (height * 0.10); 
textView4.getLayoutParams().height = (int) (height * 0.10); 
textView5.getLayoutParams().height = (int) (height * 0.10); 
textView6.getLayoutParams().height = (int) (height * 0.10); 

// to set width to each textview to 50% of screen 
textView1.getLayoutParams().width = (int) (width * 0.50); 
textView2.getLayoutParams().width = (int) (width * 0.50); 
textView3.getLayoutParams().width = (int) (width * 0.50); 
textView4.getLayoutParams().width = (int) (width * 0.50); 
textView5.getLayoutParams().width = (int) (width * 0.50); 
textView6.getLayoutParams().width = (int) (width * 0.50); 

// for margin between textviews 
space1.getLayoutParams().height = (int) (height * 0.10); 
space2.getLayoutParams().height = (int) (height * 0.10); 
space3.getLayoutParams().height = (int) (height * 0.10); 
space4.getLayoutParams().height = (int) (height * 0.10); 
space5.getLayoutParams().height = (int) (height * 0.10); 

XML Code:

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/scrollView" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fillViewport="true"> 

<RelativeLayout 
    android:id="@+id/scrollContent" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_red_dark" 
     android:text="kkjknadko" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space1" 
     android:layout_below="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space1" 
     android:text="Abcaad" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space2" 
     android:layout_below="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space2" 
     android:background="@android:color/holo_red_dark" 
     android:text="Abcd" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space3" 
     android:layout_below="@+id/textView3" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space3" 
     android:text="Abcd" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space4" 
     android:layout_below="@+id/textView4" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

    <TextView 
     android:id="@+id/textview5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space4" 
     android:background="@android:color/holo_red_dark" 
     android:text="Abcd" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space5" 
     android:layout_below="@+id/textview5" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

    <TextView 
     android:id="@+id/textview6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space5" 
     android:text="Abcd" 
     android:textColor="@android:color/black"/> 

</RelativeLayout> 

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