2017-08-12 55 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