2013-10-18 77 views
0

我嘗試創建自定義ScrollView,它將有屬性max_height,但我面臨的問題。當我設置自定義height時,ScrollView不顯示TextView

精簡版的版本我的代碼:

佈局:
TextView不會顯示在自定義滾動視圖

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<com.example.scrollviewtest.ExpandScrollView 
    android:id="@+id/scrollView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:background="#Fe6" > 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="TextView TextView TextView TextView TextView" /> 
</com.example.scrollviewtest.ExpandScrollView> 

ExpandScrollView.java

public class ExpandScrollView extends ScrollView { 

public ExpandScrollView(Context context) { 
    super(context); 
} 

public ExpandScrollView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int widthSize = MeasureSpec.getSize(widthMeasureSpec); 
    setMeasuredDimension(widthSize, 180); 

}} 

所以,我該怎麼辦錯了嗎?

回答

0

問題解決了!
我剛剛在widthSize之前加super.onMeasure(widthMeasureSpec, heightMeasureSpec);

0

只需刪除ExpandScrollView類中的onMeasure()方法。這是造成問題。 一旦你刪除它,textview將是可見的。

+0

我擴展'ScrollView'和'OnMeasure()'會有一些邏輯,它改變'height' – Dima