2017-11-17 29 views
0

这可能看起来像是一个非常常见的问题,但我似乎无法将视图移动到屏幕中心。无法将视图移动到屏幕中心(以编程方式,而不是布局属性) - Android

我有3次:

  • 的背景图像(中心作物)
  • 一个黑/白格以显现在屏幕的中心,用于调试目的。同样中心作物
  • 一个正方形,我想移动到屏幕中心正方形的中心,左上角。

我首先以像素为单位获取屏幕大小,将宽度和高度除以2得到中心的坐标。然后通过center.x和center.y移动我的方形视图 我期望广场正好移动到屏幕的中心。

我还缩放了方形视图,使其大小始终与背景图像相同。

我想以编程方式解决这个问题(不只是以布局属性为中心),因为这只是第一步,我打算能够在屏幕上的任意位置放置正方形。我试图把它放在中心,因为它似乎是最容易做的事情。

代码

public class FullScreenActivity extends AppCompatActivity { 

    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     super.onWindowFocusChanged(hasFocus); 
     if (hasFocus) { 
      getWindow().getDecorView().setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
       | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
       | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 
       | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 
       | View.SYSTEM_UI_FLAG_FULLSCREEN 
       | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY 
      ); 
     } 
    } 

} 

类白衣逻辑

public class AnimalPopupActivity extends FullScreenActivity { 

    private final String TAG = "AnimalPopup"; 

    ImageView backgroundView; 
    ImageView square; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_animal_popup); 

     backgroundView = (ImageView)findViewById(R.id.background); 

     square = (ImageView) findViewById(R.id.square); 

     DisplayMetrics metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     int screenWidthInPixels = metrics.widthPixels; 
     int screenHeightInPixels = metrics.heightPixels; 
     Log.i(TAG, "screen width pixels: "+screenWidthInPixels); 
     Log.i(TAG, "screen height pixels: "+screenHeightInPixels); 

     Point center = new Point(screenWidthInPixels/2, screenHeightInPixels/2); 
     Log.i(TAG, "center: "+center); 

     double ratioH = (double)screenWidthInPixels/100d; 
     Log.i(TAG, "ratioH: "+ratioH); 
     double ratioV = (double)screenHeightInPixels/100d; 
     Log.i(TAG, "ratioV: "+ratioV); 

     RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int)(10*ratioH), (int)(10*ratioV)); 
     layoutParams.leftMargin = center.x; 
     layoutParams.topMargin = center.y; 
     square.setLayoutParams(layoutParams); 
    } 
} 

布局

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.bentaye.app.games.AnimalPopupActivity"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ImageView 
      android:id="@+id/background" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="centerCrop" 
      app:srcCompat="@drawable/animal_popup_background" /> 

     <ImageView 
      android:id="@+id/grid" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:scaleType="centerCrop" 
      app:srcCompat="@drawable/animal_popup_grid" /> 

     <ImageView 
      android:id="@+id/square" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      app:srcCompat="@drawable/square" /> 

    </RelativeLayout> 

</android.support.constraint.ConstraintLayout> 

下面是我得到的是Nexus 5(1080×1920:420dpi) Nexus5

用下面的日志:

屏幕宽度像素:1080个

屏幕高度像素:1794

中心:点(540,897)

ratioH:10.8

ratioV:17。94

这里是我所得到的在Nexus 10(2560 * 1600:xhdpi) Nexus 10

用下面的日志:

屏幕宽度像素:1600

屏幕高度像素:2464

中心:点(800, 1232)

ratioH:16.0

ratioV:24.64

的2是关闭的几个像素,我不是一个Android专家和清楚我做错了什么,但不能明白什么。

任何帮助欢迎。干杯!

编辑:我认为这是计算考虑到状态栏。这也解释了为什么Nexus5上的差异更大,因为状态栏相对于屏幕高度更大。还有为什么这种差异仅影响垂直对齐。 一旦我把它整理出来,我会回来发布代码。

+0

为什么是我的解决方案删除了​​我最后一次编辑? – Bentaye

回答

0

假设你要为中心的广场是RelativeLayout的内,加layout_gravity=center

例如:

<ImageView 
    android:id="@+id/square" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity=center" 
    app:srcCompat="@drawable/square"/> 
+0

正如问题所述,我想以编程方式解决这个问题(不仅仅是以布局属性为中心),因为这只是第一步,我打算能够在屏幕上的任意位置放置正方形。我试图把它放在中心,因为它似乎是最容易做的事情。 – Bentaye

+0

layout_gravity = center不适用于RelativeLayout –

0

加入这一行

android:layout_centerInParent="true" 

这条线是RelativeLayout的

相关问题