To inherit view and implement the component you want, you need to use the Setmeasureddimension method, which determines the size of the current view, see Code:
View Code: Java code package cc.mdev.test; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.text.textpaint; import android.util.attributeset; import android.util.log; import android.view.View; public class myscrollview extends view { Public myscrollview (Context context, attributeset attrs) { super ( Context, attrs); } Public myscrollview (context context) { Super (context); } @Override Protected void ondraw ( Canvas canvas) { textpaint paint = new textpaint (); Paint.setantialias (true); Canvas.drawcolor(Color.gray); for (int i = 10; i < 500; i++) { canvas.drawtext ("This is the scroll text.", 10, i, paint); i = i+15; } } @Override Protected void onmeasure (Int widthmeasurespec, int heightmeasurespec) { string tag= "Onmeasure"; log.e (tag, "scroll view on measure ..."); setmeasureddimension (200, 800); } @Override Protected void onscrollchanged (Int l, int t, int oldl, int oldt) { string tag = "onscrollchanged"; log.e (tag, "Scroll ..."); super.onscrollchanged (L, t, oldl, oldt); } }
Layout files: XML code <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/" Apk/res/android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_ Parent "android:background=" #fff > <button android:text= "Button01" android:id= "@+id/button01" Android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" ></Button> <scrollview android:layout_width= "Fill_parent" android:layout_height= "fill_parent" > <cc.mdev.test.myscrollview android:layout_width= "WRAP_" Content "android:layout_height=" wrap_content "/> </ScrollView> </LinearLayout>
The effect is that the size of the custom view is
200, 800, and put into the ScrollView, ScrollView will function, if you do not use the Setmeasureddimension method, then
ScrollView will not work.