26

カスタムドローアブルを備えた SeekBar があります。Progress 要素は左側から正確にレンダリングされませんが、右側に移動されます。どうすればこれを回避できますか?

ここに画像の説明を入力

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@android:id/summary"
    android:background="@null"

    android:progressDrawable="@drawable/seekbar"
    android:thumb="@drawable/seekbar_thumb"/>

シークバー.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="@drawable/seekbar_background"/>
    <item android:id="@android:id/progress">
        <clip android:drawable="@drawable/seekbar_progress" />
    </item>

</layer-list>

seekbar_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">

    <stroke android:width="2dp" android:color="@color/colorNeutral" />

</shape>

seekbar_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">

    <stroke
        android:width="4dp"
        android:color="@color/colorAccentDark" />
</shape>

編集:

コメントで述べたように、両方の幅がある場合、同じ問題はなくなります。しかし、ドローアブルを介して2つの異なる幅で作成する必要がありますか? たぶん線だけでなく、さまざまな形で?

4

6 に答える 6

15

ストロークを描画するときにこれが問題になる理由を説明できません。ストロークの幅に関係していると思いますが、確認するソースはまだ見つかりませんでした。

オーバーレイの修正

inset目前の問題を解決するには、左側に を設定するだけです。work とシークバーの値1dpまたは2dpその両方が適切に描画されます。

注:このアプローチを使用すると、背景が短すぎたり、進行状況の値が低いと見えなくなったりするリスクはありません。

<?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="@drawable/seekbar_background"
        android:left="2dp"><!-- or 1dp -->
    </item>
    <item android:id="@android:id/progress">
        <clip android:drawable="@drawable/seekbar_progress"/>
    </item>
</layer-list>
于 2016-01-24T20:05:15.463 に答える
3

進行中の問題のように見えるので、両方とも同じ幅にする必要があると思います

しかし、進行中の幅= 4android:width="4dp"

シークバーの幅 = 2android:width="2dp"

2 または 4 になるように一致させます。異なる値を使用しないでください。

于 2016-01-22T00:41:12.933 に答える
3

幅 4dp (px に変換) の .9.png ドローアブルを使用してみてください。

上下で 1 dp を透明に保ちます。

そして、背景用に選択した中央の 2 dp に色を追加します。

このようにして、背景と進行状況の両方が同じ幅になりますが、背景は進行状況バーよりも薄く見えます。

于 2016-01-28T01:04:21.513 に答える
2

android:layout_marginLeft="-3dp" またはを使用してこのように試してください -2dp

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@android:id/summary"
    android:background="@null"
    android:layout_marginLeft="-2dp"
    android:progressDrawable="@drawable/seekbar"
    android:thumb="@drawable/seekbar_thumb"/>`
于 2016-01-28T10:58:06.570 に答える