我有一个恼人的问题,当我移动我的RelativeLayout时,其中的ImageView的大小增加,以填充整个屏幕的宽度。我用彩色背景替换了图像,因此更容易看到。
不要介意它的外观,布局是W.I.P
GIF在此处显示问题:https://gyazo.com/ae31b8e70e4f96fe89ba041549db22bb
以下是我的OnTouchListener的代码
private View.OnTouchListener relativeListener = new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent event)
{
final int X = (int) event.getRawX();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = (X - _xDelta);
layoutParams.rightMargin = -(X - _xDelta);
view.setLayoutParams(layoutParams);
break;
}
_root.invalidate();
return true;
}
};
下面是定义ImageView和RelativeLayout
的xml<LinearLayout 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/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="me.tech.myapp.Fragments.HomeFragment">
<TextView
android:id="@+id/tvCurrentTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="16dp"
android:text="StringCurrentTheme"
android:textAlignment="center"
android:textSize="18sp" />
<RelativeLayout
android:id="@+id/relativeRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<ImageView
android:id="@+id/ivMainImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:contentDescription="Foto jonguh"
android:cropToPadding="false"
android:scaleType="centerCrop"
app:srcCompat="@android:color/holo_blue_bright" />
<Button
android:id="@+id/btnReportImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignTop="@+id/ivMainImage"
android:layout_margin="16dp"
android:background="@drawable/ic_menu_send"
android:visibility="visible" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabSendImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/btnReportImage"
android:layout_alignParentBottom="true"
android:layout_marginEnd="17dp"
android:clickable="true"
app:backgroundTint="@android:color/holo_orange_dark"
app:elevation="4dp"
app:fabSize="normal"
app:srcCompat="@drawable/ic_menu_camera" />
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:0)
android:scaleType="fitCenter"
android:adjustViewBounds="true"
尝试将这些属性添加到ImageView
答案 1 :(得分:0)
如果你有
,ImageView占据整个布局是正常的<ImageView
android:id="@+id/ivMainImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
....
尝试wrap_content而不是match_parent
答案 2 :(得分:0)
通过在代码中手动更改宽度来修复它。