【发布时间】:2018-04-06 01:33:23
【问题描述】:
我使用surfaceview 创建了两个joysticksview。我想在图片中的黄色框中放置一个开关按钮。但是,当我在手机上移动操纵杆时,切换按钮会四处移动。
我的surfaceivew的xml代码如下:
<?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>
【问题讨论】:
-
将
LinearLayout的layout_height设置为match_parent可以解决它.. -
@MeowCat2012 没用,我真的找不到和我有同样问题的人。只是想确定一下,我的问题对你有意义吗?
-
layout_below="@+id/linearLayout4"但是linearLayout4在哪里?layout_below表示当前视图应放置在具有给定 id 的视图下方,此处您应使用@id/xxx而不是@+id/xxx来引用已经 现有 视图.linearLayout4和linearLayout5似乎不存在。这可能无法直接解决您的问题,但至少会让事情变得更容易。 -
感谢您的回答,对您有所帮助。我意识到,小部件移动的原因是因为我的“开关”小部件依赖于 joystickview 小部件。我在我的开关小部件 xml 代码中取出了“android:layout_below="@+id/linearLayout4"”行,它起作用了。
标签: android android-layout surfaceview