How to make a dynamic layout that is larger than screen?

前端 未结 1 1928
刺人心
刺人心 2020-12-22 04:13

I want to create a dynamically extending layout that may grow reflecting to user\'s input. First, I\'ve created an activity layout that allows scrolling in both directions

相关标签:
1条回答
  • 2020-12-22 04:34

    The solution is surprisingly simple.

    In the activity layout, a RelativeLayout node (the one with content ID) should use the custom layout class that overrides the onMeasure method.

    So here is the fix for the activity layout:

    <com.sample.MyRelativeLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clipChildren="false"
        android:clipToPadding="false" >
    </com.sample.MyRelativeLayout>
    

    and here is the class implementation:

    public class MyRelativeLayout extends RelativeLayout {
        public MyRelativeLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(0, 0);
        }
    }
    

    Note that I don't have to calculate anything, passing (0, 0) works just fine.

    Frankly speaking, I don't yet understand all pros and cons of this solution, but it works properly. The only issue that I have noticed so far is that the more items I expand, the slower the UI responds.

    I'll appreciate any comments or suggestions.

    0 讨论(0)
提交回复
热议问题