当设备方向改变时,Android ImageView位置也会改变

当设备方向改变时,Android ImageView位置也会改变,android,imageview,orientation,Android,Imageview,Orientation,我正在为我的应用程序开发自己的图库(因为图库小部件已被弃用…) 我用于此目的的所有第三方库如下所示: 我面临的问题是,当屏幕方向改变时,我的ImageView将无法保持在屏幕中心。它只是移动到了另一个位置,可能是左上角,或者其他地方 这是我的截图 方向改变前: 方向改变后: 以下是我的布局XML的关键部分: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://sch

我正在为我的应用程序开发自己的图库(因为图库小部件已被弃用…)

我用于此目的的所有第三方库如下所示:

我面临的问题是,当屏幕方向改变时,我的ImageView将无法保持在屏幕中心。它只是移动到了另一个位置,可能是左上角,或者其他地方

这是我的截图

方向改变前:

方向改变后:

以下是我的布局XML的关键部分:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF000000" android:keepScreenOn="true">
  <!-- I want my title bar and control bar in front of MorizontalPager -->
  <com.github.ysamlan.horizontalpager.HorizontalPager android:id="@+id/hPager" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">
  </com.github.ysamlan.horizontalpager.HorizontalPager>

  <LinearLayout android:id="@+id/head" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/head_bar" android:gravity="center_vertical" android:layout_alignParentTop="true">
    <Button android:id="@+id/btnBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp" android:background="@drawable/btn_back" android:textColor="#FFFFFFFF" />
    <TextView android:id="@+id/txtFolder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="@string/app_name" android:gravity="center_vertical|center_horizontal" android:layout_weight="1" android:textColor="#FFFFFFFF" />
    <Button android:id="@+id/btnMenu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/btn_menu" />
  </LinearLayout>
  ...some more...
</RelativeLayout>

……还有。。。
我活动的一部分:

private HorizontalPager hPager=null;
hPager=(HorizontalPager)findViewById(R.id.hPager);

...some code...

private void loadPhotos()
{
  for(int i=0; i<Constants.PHOTO_LIST.size(); i++)
  {
    ImageView img=new ImageView(this);
    LayoutParams lp=new LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER);
    lp.setMargins(0, 0, 0, 0);
    img.setLayoutParams(lp);
    img.setPadding(0, 0, 0, 0);
    img.setMaxWidth(Constants.SCREEN_WIDTH);
    img.setMaxHeight(Constants.SCREEN_HEIGHT);
//      img.setScaleType(ScaleType.FIT_CENTER);
    img.setAdjustViewBounds(false);
    img.setClickable(true);
    img.setFocusable(true);
    img.setFocusableInTouchMode(true);
    img.setOnClickListener(new OnClickListener()
    {
      public void onClick(View v)
      {
        isControlShow=!isControlShow;
        if(isControlShow)
        {
          head.setVisibility(View.VISIBLE);
          foot.setVisibility(View.VISIBLE);
        }
        else
        {
          head.setVisibility(View.GONE);
          foot.setVisibility(View.GONE);
        }
      }
    });
    img.setOnTouchListener(new MultiPointTouchListener());
    hPager.addView(img);
    Log.i(Constants.TAG, "Preparing photo: "+i);
    imageLoaderSet(Constants.PHOTO_LIST.get(i).getFileId(), img);
  }
  hPager.setOnScreenSwitchListener(onScreenSwitchListener);
  hPager.setCurrentScreen(currentIndex, true);
}

@Override
public void onConfigurationChanged(Configuration newConfig)
{
  super.onConfigurationChanged(newConfig);
  return;
}

@Override
protected void onResume()
{
  super.onResume();
  Log.i(Constants.TAG, "on PhotoShow resume...");
  loadPhotos();
}
private HorizontalPager hPager=null;
hPager=(HorizontalPager)findviewbyd(R.id.hPager);
…一些代码。。。
私人照片()
{

对于(int i=0;我尝试此代码..而不是在xml文件中这很奇怪,因为默认情况下,活动是在旋转时从头开始重新创建的。因此,旋转后图像应居中。您确定没有
android:configChanges
(或类似内容)吗在清单中设置?对于com.github.ysamlan.horizontalpager.horizontalpager设置属性:aligncenterinparent=“true”设置android:layout\u height=“wrap\u content”使HorizontalPager引发此异常:java.lang.IllegalStateException:ViewSwitcher只能在精确模式下使用。位于com.github.ysamlan.HorizontalPager.HorizontalPager.onMeasure(HorizontalPager.java:137)的网站还尝试设置android:layou_height=“0dip”(SDK建议将其改为包装内容),从而使HorizontalPager不可见…:-(///再次尝试,将android:layout\u centerInParent=“true”添加到HorizontalPager,没有帮助…:-(顺便说一句,我尝试了以下代码ImageView img=new ImageView(this);LayoutParams lp=new LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_父项,android.view.ViewGroup.LayoutParams.MATCH_父项);lp.addRule(RelativeLayout.CENTER_父项);lp.setMargins(0,0,0,0);img.setLayoutParams(lp);…hPager.addView(img);也不起作用。