安卓 SeekBar 拇指图片未正确显示

4
我创建了一个自定义的拖动条滑块,它在Android Studio预览中看起来非常好,但是在我的设备上运行应用程序时,它却显示错误!
应该长成这样: what it should look like 但实际上在我的设备上看起来像这样: what it actually look like on my device 以下是我的seekbar_thumb.xml代码:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:height="35dp" android:width="35dp" >
    <shape android:shape="oval">
        <solid android:color="#fff" />
    </shape>
</item>
<item android:gravity="center" android:height="15dp" android:width="15dp">
    <shape android:shape="oval">
        <solid android:color="@color/colorPrimary" />
    </shape>
</item>

我的SeekBar代码
        <SeekBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar"
        android:max="100"
        android:progress="50"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:thumb="@drawable/seekbar_thumb"
        android:progressDrawable="@drawable/progress_bar_background"
        android:layout_marginBottom="@dimen/seekbar_margin_bottom"
        android:layout_marginRight="@dimen/seekbar_margin_rt"
        android:layout_marginLeft="@dimen/seekbar_margin_lt"
        android:thumbOffset="0dp"/>
1个回答

5
请在drawable文件夹中尝试使用seekbar_thumb.xml。
    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
    <shape android:shape="oval">
    <solid android:color="#00f"/>
    <size
    android:width="15dp"
    android:height="15dp"/>
    </shape>
    </item>
    <item>
    <shape android:shape="oval">
    <stroke android:color="@android:color/transparent"
    android:width="5dp"/>
    <solid android:color="#f00"/>
    <size
    android:width="10dp"
    android:height="10dp"/>
    </shape>
    </item>
    </layer-list>

还有activity_main.xml文件

    <RelativeLayout     
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="40dp">

    <SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/seekBar"
    android:max="100"
    android:progress="50"
    android:thumb="@drawable/seekbar_thumb" />

    </RelativeLayout>

工作得很好!你能解释一下为什么这个有效而我的第一个不行吗? - Bialy
1
你必须在<size/>中定义高度和宽度,而不是直接在<item/>中定义。另外,我只需要用半径更小的另一个圆,并将其实心颜色设置为以下内容: <solid android:color="#f00"/> 与你所做的一样,但要在<size/>中定义所有高度和宽度参数。 - Abhay Dhiman
1
你的代码在安卓6设备上运行良好,但对于低版本设备,定义<size>非常重要。 - Abhay Dhiman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接