我有一个复合视图。我有一个ImageButton,我希望能够左右拖动。我可以将它向左和向上拖动它的原始位置,它看起来很好。如果我向右或低于它的原始位置,按钮就会消失。我已启用开发人员选项来绘制视图边界。当我移动ImageButton时,我看到左边界和顶边界扩展和缩小。右边和下边没有。
这里正在向右移动它的原始边界,你可以看到它在越过右边时消失了。
我尝试在按钮上设置新的布局参数,但看到位置没有变化。所以我使用的是setLeft()和setBottom()。为什么我的按钮消失了?
@OnTouch(R.id.silence_snooze_choice_button)
public boolean choiceButtonClicked(View v, MotionEvent event){
//LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) choiceButton.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int)event.getRawX();
int y_cord = (int)event.getRawY();
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Log.i(TAG, String.format("%d,%d", x_cord, y_cord));
//this didn't work.
//layoutParams.leftMargin = x_cord -25;
//layoutParams.topMargin = y_cord - 75;
//choiceButton.setLayoutParams(layoutParams);
choiceButton.setLeft(x_cord);
choiceButton.setBottom(y_cord);
break;
default:
break;
}
return true;
}
这是我查看顶部的布局。 CircleDragSelector是复合视图的名称。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="180dp"
android:background="@color/primary"
>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_margin="16dp"
android:layout_centerVertical="true"
android:src="@drawable/abc_ic_menu_paste_mtrl_am_alpha"
/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:layout_centerVertical="true"
android:src="@drawable/ic_action_share"
/>
<ImageButton
android:id="@+id/silence_snooze_choice_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_play_circle_outline"
/>
</RelativeLayout>
这是父布局
<?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"
>
<SurfaceView
android:id="@+id/surface_view_camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<View
android:id="@+id/view_transparent_overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:alpha="0.85"/>
<TextView
android:id="@+id/alarm_screen_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:textSize="36sp"
android:textColor="@color/accent_material_dark"
android:text="Alarm name" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/alarm_screen_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/alarm_screen_name"
android:layout_marginTop="28dp"
android:layout_centerHorizontal="true"
android:text="00 : 00"
android:textSize="52dp"
android:textColor="@color/accent_material_dark"
/>
<ImageButton
android:id="@+id/alarm_screen_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:src="@drawable/ic_cancel_black_48dp"
android:background="@null"
android:text="Dismiss"
android:textSize="28dp" />
<com.example.tyler.rollout.views.CircleDragSelector
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.example.tyler.rollout.views.CircleDragSelector>
</RelativeLayout>
答案 0 :(得分:1)
默认情况下,布局会剪切其子项,这意味着默认情况下会隐藏负边距等内容。
放
<RelativeLayout
...
clipChildren="false">
父布局上的应该解决这个问题。