我在一个布局上有 2 个网络视图。我希望在触摸时,触摸的视图会以牺牲另一个视图为代价变得更大(更宽),我该怎么做?
谢谢!
子类 WebView:
public class MyWebView extends WebView{
private MyActivity mActivity;
//Call this on each webview in activity's onCreate after
//instantiating the web views. i.e. after setContentView()
//and assigning it to a variable with findViewById() or
//after programmatically creating the web view.
public setActivity(MyActivity activity){
mActivity = activity;
}
@Override
public boolean onTouchEvent(MotionEvent event){
if (event.getAction()==ACTION_POINTER_DOWN && mActivity!=null){
mActivity.onWebviewTouched(self);
}
super.onTouchEvent(event);
}
}
在您的活动中:
public void onWebviewTouched(MyWebView webView) {
if (webView == mWebviewLeft){
//grow left webview if it isn't already grown, and shrink right webview.
} else if (webView == mWebviewRight) {
//grow right webview if it isn't already grown, and shrink left webview.
}
}
使用 Animation 类及其子类平滑地修改视图的宽度。如果使用 LinearLayout,您可以将一个视图的布局权重设置为一个常数值,并简单地为另一个视图的布局权重设置动画,使其大于或小于该常数值。
如果您只想即时更改,只需使用 LayoutParams 直接设置视图宽度。