0

再会。

我有一个 LinearLayout 位图框,我在其中通过 SandboxView 类放置可绘制对象,如下所示:

LinearLayout myLayout = (LinearLayout) mRoot.findViewById(R.id.bitmapBox);
View view = new SandboxView(this.getActivity(), bitmap);
myLayout.addView(view);

在父布局中,我也有一个水平滚动视图,但我想通过触摸画布使其可见/不可见。我试图这样做:

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;
  }
}
});

“视图”不是在画布上保存位图的对象吗?因为这行不通。我不知道使用什么视图来设置 onClick 侦听器,有什么建议吗?

4

2 回答 2

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);
        }
    }
});
于 2012-08-13T15:13:48.480 回答
0

确保将布局的可点击属性设置为 true。

于 2012-08-13T14:55:13.780 回答