美好的一天。
我有一个LinearLayout位图框,我在其中通过SandboxView类放置可绘制对象:
LinearLayout myLayout = (LinearLayout) mRoot.findViewById(R.id.bitmapBox);
View view = new SandboxView(this.getActivity(), bitmap);
myLayout.addView(view);
在父布局中我也有一个水平scrollView,但我想通过触摸画布使其可见/不可见。我试着这样做:
view.setOnClickListener(new OnClickListener() {
int x = 0;
@Override
public void onClick(View view) {
HorizontalScrollView myLayout = (HorizontalScrollView) mRoot.findViewById(R.id.hide);
if (x == 0) {
myLayout.setVisibility(1);
myLayout.setVisibility(View.GONE);
x = 1;
}
else {
myLayout.setVisibility(View.VISIBLE);
x = 0;
}
}
});
“view”不是在画布上保存位图的对象吗?因为这不起作用。我不知道用什么视图来设置onClick监听器,有什么建议吗?
答案 0 :(得分:1)
你的onClickListener可以稍微清理一下:
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// no need to cast to horizontalscrollview here since we are just setting visibility which is available to anything extending the View class.
View mView = mRoot.findViewById(R.id.hide);
if (mView == null) {
return; // do nothing
} else if (mView.getVisibility() == View.GONE) {
mView.setVisibility(View.VISIBLE);
} else if (mView.getVisibility() == View.VISIBLE) {
mView.setVisibility(View.GONE);
}
}
});
答案 1 :(得分:0)
确保将布局的可点击属性设置为true。