android关联布局,如何在android中保持并拖动(重新定位)布局及其关联的布局
我正在制作一个Android应用程序.我有一个场景.首先看下面的屏幕截图,所以任何一个人来这里回答有一些清晰的图片,我想要的. 场景:活动在导航栏下面有一个地图,在中心有一个RelativeLayout(红色背景),在Red RelativeLayout下有一个ListView我想要的是:我想拖动或重新定位(无论其他人使用什么术语)Red RelativeLayout通过我的手指按住它并在屏幕上上
我正在制作一个
Android应用程序.我有一个场景.首先看下面的屏幕截图,所以任何一个人来这里回答有一些清晰的图片,我想要的.
场景:活动在导航栏下面有一个地图,在中心有一个RelativeLayout(红色背景),在Red RelativeLayout下有一个ListView
我想要的是:我想拖动或重新定位(无论其他人使用什么术语)Red RelativeLayout通过我的手指按住它并在屏幕上上下移动以及下面的listview也应该随着这个布局移动
我试过的代码:
xml布局
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="@dimen/fifty_five_dp"
android:id="@+id/topBar"
android:background="@color/bg_color" >
android:id="@+id/button_menu"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="4dp"
android:background="@drawable/list_btn"
android:onClick="toggleMenu" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Watchover"
android:textColor="@android:color/white"
android:textSize="@dimen/eighteen_sp"
android:textStyle="bold" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/topBar"
android:id="@+id/root"
android:background="@android:color/white" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" >
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/greenBarImage" />
android:id="@+id/redLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/forty_dp"
android:layout_centerVertical="true"
android:background="@color/btn_color" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Fences Info"
android:textColor="@android:color/white"
android:textSize="@dimen/sixteen_sp" />
android:id="@+id/btn_drag"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="4dp"
android:background="@drawable/list_btn"
android:onClick="toggleMenu"
android:visibility="gone" />
android:id="@+id/list_devices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/greenBarImage"
android:padding="@dimen/five_dp"
android:scrollbars="none" />
Java代码:
public class MyActivity extends FragmentActivity implements OnTouchListener
{
private int _xDelta;
private int _yDelta;
RelativeLayout redLayout;
FrameLayout rootLayout;
ListView devicesList;
@Override
protected void onCreate(Bundle arg0)
{
super.onCreate(arg0);
setContentView(R.layout.main_screen);
devicesList = (ListView) findViewById(R.id.list_devices);
rootLayout = (FrameLayout) findViewById(R.id.root);
redLayout = (RelativeLayout) findViewById(R.id.redLayout);
redLayout.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent event)
{
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
rootLayout.invalidate();
return true;
}
}
获取上述代码的来源帮助here
我尝试了代码没有得到理想的结果.当我向上和向下拖动红色RelativeLayout时,listview覆盖在该布局上并且未按我的意愿调整.任何类型的帮助将不胜感激.
更多推荐
所有评论(0)