簡體   English   中英

Android:制作可滾動的自定義視圖

[英]Android: make a scrollable custom view

我已經滾動了自己的自定義視圖,可以繪制到屏幕上,但是我真正想做的是將屏幕的measuredHeigh設置為例如1000px,並讓用戶在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,awakenScrollBars等,但無濟於事。 我是否錯過了一些愚蠢的事情,還是犯了一些根本性的錯誤?

先感謝您,

馬丁

加成:

我嘗試使用以下布局文件將其添加為自定義組件,並使用setContentView(R.layout.exampleLayout)更改TestScreen的代碼以指向正確的資源,但這會導致模擬器崩潰。 我嘗試將代碼注釋到最低限度,但仍然崩潰,因此我正在做一些根本錯誤的事情,但是我不確定它是什么:

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

只需將您的視圖放在ScrollView中即可!

請注意,ScrollWiew應該是根節點(這里是您的LinearLayout)

如果您想自己滾動,則可以在覆蓋自定義視圖的onTouchEvent方法的同時使用ScrollerVelocityTracker 為了幫助您實現它,我建議您仔細閱讀這些類的Javadocs,並看一下像NumberPicker一樣滾動的android小部件實現。

如果您希望整個活動都是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>

然后,使用此布局的活動可能如下所示:

public class ScrollingActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.scroll_layout);
    }
}

這假定您的xml文件的名稱為scroll_layout.xml。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM