首页 > 解决方案 > Android多个垂直seekBars

问题描述

我正在尝试为应该有 2 个垂直 SeekBars 的 Android 应用程序设计一个视图,这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/control_view_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#0099cc"
    android:orientation="horizontal"
    tools:context=".FullscreenActivity">

    <SeekBar
        android:id="@+id/seekBarL"
        android:rotation="270"
        android:splitTrack="false"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

    <SeekBar
        android:id="@+id/seekBarR"
        android:rotation="270"
        android:splitTrack="false"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
</LinearLayout>

在 android studio 的预览版和设备上,它看起来还不错,但是每当我尝试更改 left 的位置时SeekBar,就会触发 right 的位置。无论我把拇指放在哪里,唯一正确的都会做出反应。有没有人遇到过这种奇怪的行为?

标签: androidandroid-layoutseekbar

解决方案


我不知道为什么会发生这种情况(这很奇怪),但我知道如何修复它。不要将 SeekBars 放在同一个容器中,放入FrameLayout它就会起作用。

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/control_view_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099cc"
    android:orientation="horizontal">

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <SeekBar
            android:id="@+id/seekBarR"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:rotation="270" />

    </FrameLayout>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <SeekBar
            android:id="@+id/seekBarL"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:rotation="270" />

    </FrameLayout>

</androidx.appcompat.widget.LinearLayoutCompat>

推荐阅读