【问题标题】:Android: make a scrollable custom viewAndroid:制作可滚动的自定义视图
【发布时间】:2016-11-30 18:55:47
【问题描述】:

我已经推出了自己的自定义视图,并且可以在屏幕上绘制,但我真正想做的是将屏幕的测量高度设置为 1000 像素,然后让用户在 Y 轴上滚动,但我在这样做时遇到了问题。任何人都可以帮忙吗?

这里有一些代码:

public class TestScreen extends Activity  {
     CustomDrawableView mCustomDrawableView;
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);    
         mCustomDrawableView = new CustomDrawableView(this);
         setContentView(mCustomDrawableView);
     }
 }

public class CustomDrawableView extends View {

    public CustomDrawableView(Context context) {
        super(context);
        setVerticalScrollBarEnabled(true);
        setMinimumHeight(1000);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawLine(...);
        // more drawing
    }
}

我尝试通过调用 super 来覆盖 scrollTo、scrollBy、wakeScrollBars 等,但无济于事。我错过了什么愚蠢的东西,还是我犯了一些根本性的错误?

提前谢谢你,

马丁

加法:

我尝试将其添加为具有以下布局文件的自定义组件,并将TestScreen 中的代码更改为使用setContentView(R.layout.exampleLayout) 指向正确的资源,但这会导致模拟器崩溃。我尝试将代码注释到最低限度,但它仍然崩溃,所以我正在做一些根本性的错误,但我不确定它是什么:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
       <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent">
            <com.martyn.testApp.CustomDrawableView
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
             />
       </ScrollView>
</LinearLayout>

【问题讨论】:

    标签: android view


    【解决方案1】:

    只需将您的视图放在 ScrollView 中!

    注意 ScrollWiew 应该是根节点 (这里是你的 LinearLayout)

    【讨论】:

    • 这是我最终采用的方法。我有一个 LinearLayout 作为布局 xml 中的基本节点,然后是一个 Scrollview,其中包含我的自定义视图
    • @Moons 我的滚动视图中有一个线性布局,但是当我添加一个图像视图时,它确实出现在滚动中,但是当我向它添加我的自定义视图时,它没有......但是它当我将我的视图设置为我的活动的内容视图时会显示它只是显示我的视图......所以它工作......那么为什么它不能在滚动视图中工作?
    【解决方案2】:

    如果您希望整个 Activity 成为 ScrollView,请执行以下操作:

    <?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>
    

    那么使用这个布局的activity可能看起来像这样:

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

    这假定您的 xml 文件的名称是 scroll_layout.xml。

    【讨论】:

      【解决方案3】:

      如果您想自己进行滚动,可以使用ScrollerVelocityTracker,同时覆盖自定义视图的onTouchEvent 方法。为了帮助您实现它,我建议查看这些类的 Javadocs 并查看像 NumberPicker 那样滚动的 android 小部件实现。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多