【问题标题】:Widgets gets shifted around by surfaceview in a relative layout android小部件在相对布局android中被surfaceview移动
【发布时间】: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>

【问题讨论】:

  • LinearLayoutlayout_height 设置为 match_parent 可以解决它..
  • @MeowCat2012 没用,我真的找不到和我有同样问题的人。只是想确定一下,我的问题对你有意义吗?
  • layout_below="@+id/linearLayout4" 但是linearLayout4在哪里? layout_below 表示当前视图应放置在具有给定 id 的视图下方,此处您应使用 @id/xxx 而不是 @+id/xxx 来引用已经 现有 视图. linearLayout4linearLayout5 似乎不存在。这可能无法直接解决您的问题,但至少会让事情变得更容易。
  • 感谢您的回答,对您有所帮助。我意识到,小部件移动的原因是因为我的“开关”小部件依赖于 joystickview 小部件。我在我的开关小部件 xml 代码中取出了“android:layout_below="@+id/linearLayout4"”行,它起作用了。

标签: android android-layout surfaceview


【解决方案1】:

如果您有一个像操纵杆这样的移动小部件。确保其他布局或小部件不依赖于操纵杆小部件。例如,在我的开关小部件 xml 代码中,我有 android:layout_below="@+id/linearLayout4 之类的东西,linearLayout4 是操纵杆布局。因此,当我的操纵杆移动时,我的开关小部件布局也会随之移动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多