Android: make a scrollable custom view

前端 未结 3 1510
春和景丽
春和景丽 2021-01-02 13:28

I\'ve rolled my own custom view and can draw to the screen alright, but what I\'d really like to do is set the measuredHeigh of the screen to, say, 1000px and let the user s

相关标签:
3条回答
  • 2021-01-02 14:19

    If you are wanting the entire activity to be a ScrollView, then do something like this:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/android:list"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" >
    
      <LinearLayout android:id="@+id/scrollBody"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <TextView android:id="@+id/textView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/string1" />
    
        <TextView android:id="@+id/textView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/string2" />
    
      </LinearLayout>
    
    </ScrollView>
    

    Then the activity that uses this layout can look something like this:

    public class ScrollingActivity extends Activity
    {
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
    
            this.setContentView(R.layout.scroll_layout);
        }
    }
    

    This assumes that the name of your xml file is scroll_layout.xml.

    0 讨论(0)
  • 2021-01-02 14:20

    Just put your view in a ScrollView!

    Note that the ScrollWiew should be the root node (here it's your LinearLayout)

    0 讨论(0)
  • 2021-01-02 14:28

    If you want to do scrolling on your own, you can use a Scroller and a VelocityTracker while overriding the onTouchEvent method of your custom view. To help you implement it I recommend looking through the Javadocs of these classes and taking a look at android widgets implementations that do scrolling like the NumberPicker.

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