2017-08-12 57 views
0

我正在制作一个应用程序,其中RelativeLayout完全适合屏幕。我已经添加了一个按钮,使用setScaleX()setScaleY()来缩放此布局。因此(显然)整个布局不再适合屏幕。所以我首先想到使用ScrollView可以在RelativeLayout中移动,但这不起作用。有什么建议么?提前致谢。在RelativeLayout中移动android

下面是XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/layoutMain" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="phou.minesweeper.GameActivity"> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/chronometer" 
    android:layout_below="@+id/textViewBest"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_centerHorizontal="true" 
     android:layout_centerInParent="true" 
     android:layout_centerVertical="true"> 

     <RelativeLayout 
      android:id="@+id/grid" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerInParent="true" 
      android:layout_centerVertical="true"></RelativeLayout> 
    </ScrollView> 
</RelativeLayout> 
</RelativeLayout> 

其中即时缩放RelativeLayout是具有ID =网格。 xml代码太短了,因为所有视图都是用java创建的。

+0

向我展示你的xml代码。 –

回答

1

当您使用setScaleX/Y在Java中操作视图大小时,您可能需要在RelativeLayout上实现onTouchListener以处理拖动动作事件,以便以编程方式移动整个布局。

假设您缩放的视图是@ id/grid,一个简单的OnTouchListener会看起来像这样。

final View grid = findViewById(R.id.grid); 
final Point dispSize = new Point(); 
getWindowManager().getDefaultDisplay().getSize(dispSize); 
grid.setOnTouchListener(new OnTouchListener() { 

    private PointF mAnchor = new PointF(); 
    private PointF mTouchPoint = new PointF(); 

    @Override 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     mTouchPoint.set(motionEvent.getX(), motionEvent.getY()); 
     switch (motionEvent.getActionMasked()) { 
      case MotionEvent.ACTION_DOWN: 
       mAnchor.set(mTouchPoint); 
       return true; 
      case MotionEvent.ACTION_MOVE: 
       float dx = mTouchPoint.x - mAnchor.x; 
       float dy = mTouchPoint.y - mAnchor.y; 
       float left = grid.getX() + dx; 
       float right = left + grid.getWidth(); 
       float top = grid.getY() + dy; 
       float bottom = top + grid.getHeight(); 
       if (grid.getWidth() > dispSize.x) { 
        if (left > 0f) { 
         left = 0f; 
        } 
        if (right < dispSize.x) { 
         left += dispSize.x - right; 
        } 
       } 
       if (grid.getHeight() > dispSize.y) { 
        if (top > 0f) { 
         top = 0f; 
        } 
        if (bottom < dispSize.y) { 
         top += dispSize.y - bottom; 
        } 
       } 
       grid.setX(left); 
       grid.setY(top); 
       return true; 
      default: 
       return true; 
     } 
    } 
}); 

注意这OnTouchListener就收到的只是没有被截获由RelativeLayout的(我们不会在XML代码中看到的)内容视图中的触摸事件。这意味着内容视图需要通过在自己的触摸/手势侦听器中返回false来将任何未消耗的触摸事件传递到层次结构上。

+0

感谢您的回答,但我仍然有这个问题。它的移动量没有限制。我可以将布局拖出屏幕。你有这个解决方案吗?谢谢 –

+0

最初的答案只是为了说明你在onTouchListener中可以做什么。我已经编辑了答案,以显示一个简单的方法来将网格保持在屏幕限制内。您的具体要求可能需要进一步改进,但希望您能明白。 – AGDownie