具有顶部/底部属性的项目的层列表 [英] Layer-List with items having top/bottom properties

查看:86
本文介绍了具有顶部/底部属性的项目的层列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按以下方式自定义SeekBar的视图:

I am trying to customize the view of the SeekBar as follows:

<SeekBar
    android:id="@id/seek_bar_player"
    android:layout_width="match_parent"
    android:layout_height="6pt"
    android:paddingLeft="0pt"
    android:paddingRight="0pt"
    android:paddingStart="0pt"
    android:paddingEnd="0pt"
    android:layout_marginBottom="-2pt"
    android:layout_above="@id/player_container"
    android:progressDrawable="@drawable/seekbar_progress"
    android:thumb="@drawable/seekbar_thumb"
    android:padding="0pt"
    />

seekbar_progress.xml的内容是

The content of seekbar_progress.xml is,

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/background"
        android:bottom="2pt"
        android:top="2pt">
        <color android:color="@color/seekbarBackground" />
    </item>

    <item
        android:id="@android:id/progress"
        android:bottom="2pt"
        android:top="2pt">
        <clip>
            <color android:color="@color/seekbarProgressBackground" />
        </clip>
    </item>
</layer-list>

,seekbar_thumb.xml的内容是

and the content of the seekbar_thumb.xml is,

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:width="2pt"
        android:height="6pt">
        <color android:color="@android:color/holo_red_dark" />
    </item>
</layer-list>

这会在API 25虚拟设备上产生期望的结果,如下所示:

This results in the desired outcome on API 25 virtual device as:

但是,在带有Android 5.1.1(API级别22)的Levovo Vibe C上的结果却很奇怪:

However, its outcome on a Levovo Vibe C with Android 5.1.1 (API Level 22) is weird:

这可能是什么原因?任何建议都将受到高度赞赏.

What may be the reason of this? Any suggestion is highly appreciated.

推荐答案

原因是item android:widthandroid:height在API23之前不可用.密切注意Android Studio中的此类提示:

The reason is item android:width and android:height are not available until API23. Keep an eye on Android Studio for hints like this:

因此,在较旧的API22设备上,它的行为就好像您根本没有设置它们一样.

Therefore on the older, API22 device, it behaves as though you haven't set them at all.

这是seekbar_thumb.xml文件的替代方法:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="@android:color/holo_red_dark"/>
    <size
        android:width="2pt"
        android:height="6pt"/>
</shape>

厚度差异可能归因于使用pt而不是dp,请参见 Android文档.

The thickness differences could be down to the use of pt over dp, see here and the android documentation.

seekbar_progress.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/background"
        android:drawable="@color/seekbarBackground"/>
    <!-- secondary progress optional -->
    <item android:id="@android:id/secondaryProgress">
        <scale
            android:drawable="#00ffff"
            android:scaleWidth="100%"/>
    </item>
    <item android:id="@android:id/progress">
        <scale
            android:drawable="@color/seekbarProgressBackground"
            android:scaleWidth="100%"/>
    </item>
</layer-list>

请勿在该文件中完全指定尺寸.

Do not specify dimensions at all in that file.

用法:

<SeekBar
    ...
    android:maxHeight="2pt"
    android:progressDrawable="@drawable/seekbar_progress"
    android:thumb="@drawable/seekbar_thumb"
    />

maxHeight限制条的高度,但不限制拇指的高度.

The maxHeight is restricting the bar's height, but not the thumb.

同样,除非您有充分的理由,否则我强烈建议您使用dp而不是pt.

Again I highly recommend using dp not pt unless you have a good reason.

这篇关于具有顶部/底部属性的项目的层列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆