小部件在相对布局中被 surfaceview 移动 android
Widgets gets shifted around by surfaceview in a relative layout android
我使用 surfaceview 创建了两个 joysticksview。我想放置一个开关按钮,如图中黄色框所示。但是,当我在 phone 上移动操纵杆时,开关按钮会移动。
我的xml surfaceivew 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/background_v2"
tools:context="com.example.android.java.JoyStickActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:id="@+id/linearLayout">
<com.example.android.java.JoyStickView
android:id="@+id/joystickLeft"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_alignParentStart="true"
android:layout_weight="0.5" />
<com.example.android.java.JoyStickView
android:id="@+id/joystickRight"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="0.5" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/linearLayout4"
android:layout_marginStart="39dp"
android:layout_marginTop="42dp"
android:layout_toEndOf="@+id/linearLayout5"
android:layout_weight="1">
<Switch
android:id="@+id/turbo_switch_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="85dp"
android:layout_marginTop="14dp"
android:rotation="90"
android:scaleX="1"
android:scaleY="0.8"
android:thumb="@drawable/thumb"
android:track="@drawable/track" />
</RelativeLayout>
</RelativeLayout>
如果您有像操纵杆这样的移动小部件。确保其他布局或小部件不依赖于操纵杆小部件。例如,在我的开关部件 xml 代码中,我有类似 android:layout_below="@+id/linearLayout4
的东西,而 linearLayout4
是操纵杆布局。因此,每当我的操纵杆移动时,我的开关小部件布局也将始终移动。
我的xml surfaceivew 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/background_v2"
tools:context="com.example.android.java.JoyStickActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:id="@+id/linearLayout">
<com.example.android.java.JoyStickView
android:id="@+id/joystickLeft"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_alignParentStart="true"
android:layout_weight="0.5" />
<com.example.android.java.JoyStickView
android:id="@+id/joystickRight"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="0.5" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/linearLayout4"
android:layout_marginStart="39dp"
android:layout_marginTop="42dp"
android:layout_toEndOf="@+id/linearLayout5"
android:layout_weight="1">
<Switch
android:id="@+id/turbo_switch_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="85dp"
android:layout_marginTop="14dp"
android:rotation="90"
android:scaleX="1"
android:scaleY="0.8"
android:thumb="@drawable/thumb"
android:track="@drawable/track" />
</RelativeLayout>
</RelativeLayout>
如果您有像操纵杆这样的移动小部件。确保其他布局或小部件不依赖于操纵杆小部件。例如,在我的开关部件 xml 代码中,我有类似 android:layout_below="@+id/linearLayout4
的东西,而 linearLayout4
是操纵杆布局。因此,每当我的操纵杆移动时,我的开关小部件布局也将始终移动。