2015-12-23 25 views
3

我有一個與Android編程的問題。Android 1父ScrollView,3與垂直滾動TextView

我有一個父滾動視圖的整個活動,並有三個滾動功能textviews。但是,當我使用下面的代碼時,它似乎根本不工作。只有父滾動可用。

 final View view = inflater.inflate(R.layout.activity_register_terms_fragment, container, false); 
     TextView basicTermView = (TextView) view.findViewById(R.id.register_terms_basic_info); 
     TextView purposeTermView = (TextView) view.findViewById(R.id.register_terms_purpose_info); 
     TextView provideTermView = (TextView) view.findViewById(R.id.register_terms_provide_info); 
     TextView previous = (TextView) view.findViewById(R.id.register_terms_pre); 
     TextView next = (TextView) view.findViewById(R.id.register_terms_next); 

     basicTermView.setMovementMethod(new ScrollingMovementMethod()); 
     purposeTermView.setMovementMethod(new ScrollingMovementMethod()); 
     provideTermView.setMovementMethod(new ScrollingMovementMethod()); 

如何更改密碼? 謝謝你幫助我!

回答

4

ScrollView內不能有可滾動的視圖,如TextViewListViewRecyclerView。因此,在普通佈局中使用簡單的TextView並向其添加android:scrollbars屬性,或者可以使用視圖的自定義類,它將以編程方式計算視圖的寬度/高度,並使用ScrollView作爲父級。

作爲一個例子,在scrollview中使用Listview,我們需要使用下面的定製類Listview,它將計算列表項的高度並進行設置。

public class ExpandedListView extends ListView { 

private ViewGroup.LayoutParams params; 
private int old_count = 0; 

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

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

public ExpandedListView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

@Override 
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
       Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom); 
     ViewGroup.LayoutParams params = getLayoutParams(); 
     params.height = getMeasuredHeight();  
} 
@Override 
protected void onDraw(Canvas canvas) { 
    if (getCount() != old_count) { 
     this.setScrollContainer(false); 
     old_count = getCount(); 
     params = getLayoutParams(); 
     params.height = getCount() 
       * (old_count > 0 ? getChildAt(0).getHeight() : 0); 
     setLayoutParams(params); 
    } 

    super.onDraw(canvas); 
} 

} 
+0

這是什麼? **你可以使用視圖的定製類**可以詳細說明嗎? @Beena – DJphy

+0

是的。我編輯了我的答案。你可以看看。 – Beena