2016-07-06 161 views
1

我是新Android开发,我挣扎了这么多我的ScrollView,其实我想做到以下几点:如何动态清除和设置ScrollView的内容?

  • 设置其内容到我的布局之一,例如:scrollView.setContent(R.layout.activity_payment)
  • 明确其内容,例如:scrollView.clearContent()

有人能告诉我如何达到上述说明的代码吗?

回答

2

其实这是很简单的:

覆盖的ScrollView

public class ClearScrollView extends ScrollView 

添加两种方法:

public void setContent(@LayoutRes int layoutRes) { 
    View view = LayoutInflater.from(getContext()).inflate(layoutRes, this, false); 
    this.addView(view); 
} 

public void clearContent() { 
    this.removeAllViews(); 
} 

,这是所有。在任何需要的地方使用ScrollView。