android開發中scrollview添加自定義view的滑動顯示問題


最近做了一個實戰用到自定義view,由於view比屏幕大所以想放到scrollview中,如下程序。發現不顯示。於是對scrollview進行了研究。

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2 xmlns:tools="http://schemas.android.com/tools"
 3 android:layout_width="match_parent"
 4 android:layout_height="match_parent"
 5 tools:context=".MainActivity" 
 6 android:orientation="vertical"
 7 android:weightSum="35">
 8 
 9 <LinearLayout
10 android:layout_width="match_parent"
11 android:layout_height="0dp"
12 android:layout_weight="2"
13 android:orientation="horizontal"
14 
15 >
16 </LinearLayout>
17 <ScrollView
18 android:id="@+id/scrollView1"
19 android:layout_width="match_parent"
20 android:layout_height="0dp" 
21 android:fillViewport="true"
22 android:layout_weight="30">
23 
24 <LinearLayout
25 android:id="@+id/linearLayout1"
26 android:layout_width="match_parent"
27 android:layout_height="wrap_content" >
28 
29 <com.example.android_draw.Mydraw
30 android:id="@+id/mydraw1"
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content" />
33 
34 </LinearLayout>
35 </ScrollView>
36 <LinearLayout
37 android:layout_width="match_parent"
38 android:layout_height="0dp"
39 android:layout_weight="3"
40 android:orientation="vertical" >
41 </LinearLayout>
42 
43 </LinearLayout>

 

理論部分

  1、ScrollView和HorizontalScrollView是為控件或者布局添加滾動條

  2、上述兩個控件只能有一個孩子,但是它並不是傳統意義上的容器

  3、上述兩個控件可以互相嵌套

  4、滾動條的位置現在的實驗結果是:可以由layout_width和layout_height設定

  5、ScrollView用於設置垂直滾動條,HorizontalScrollView用於設置水平滾動條:需要注意的是,有一個屬性是    scrollbars 可以設置滾動條的方向:但是ScrollView設置     成horizontal是和設置成none是效果同,HorizontalScrollView設置成vertical和none的效果同。

  6. ScrollView要求其只有一個子View。當有多個View時,可以使用LinearLayout等布局包含,使其直接子View只有一個。

  7,scrollview會對其內部的view大小進行判斷(長寬為零時可能不執行),以便給予顯示空間,決定是否顯示滑動條。

 

所以在scrollview里添加自定義view時,我們給自定義view設置大小。

方法一:在.xml文件直接設置

  

29 <com.example.android_draw.Mydraw
30 android:id="@+id/mydraw1"
31 android:layout_width="wrap_content"
32 android:layout_height="600dp" />

方法二:在自定義view中 重寫onMeasure方法。

 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)介紹:

  測量View及其Content,確定measuredWidth和measuredHeight。在方法measure(int, int)中調用。重寫onMeasure方法時,需要調用方法setMeasuredDimension(int, int),存儲View的measuredWidth和measuredHeight。若存儲失敗,方法measure(int, int)會拋出異常IllegalStateException。可以調用super.onMeasure(int, int)方法。

  除非MeasureSpec准許更大的size,否則measure的默認實現是background size。子類重寫onMeasure(int, int)提供Content的更佳測量。如果onMeasure被重寫,子類必須保證measuredWidth和measuredHeight至少是view的minHeight和minWidth。minHeight/Width通過getSuggestedMinimumHight/Width()獲取。

參數width/heightMeasureSpec表示parent強加的horizontal/vertical space要求。

  在自定義view中寫如下代碼,

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    setMeasuredDimension(寬度, 高度);
    }

結束。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM