103

特定の側面のみにストロークを付けて Android シェイプ オブジェクトを作成することはできますか?

たとえば、私は持っています:

<stroke 
 android:width="3dip" 
 android:color="#000000"
    android:dashWidth="10dip" 
    android:dashGap="6dip" />

これは次の CSS に似ています。

border: 3px dashed black;

ストロークを片側だけに設定するにはどうすればよいですか? これは私がCSSでそれを行う方法です:

border-left: 3px dashed black;

これを Android XML でどのように行うのですか?

4

8 に答える 8

134

私はこれで良い解決策を達成しました:

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
    <item android:top="-1dp" android:right="-1dp" android:left="-1dp">
      <shape>
            <solid android:color="@android:color/transparent" />
            <stroke android:width="1dp" android:color="#ffffff" />
      </shape>
    </item>

</layer-list>

これは、透明な背景が必要であるが、まだ開いたストロークの色が必要な場合にうまく機能します(私の場合、必要なのは最終行だけでした)。背景色が必要な場合は、マラグの回答のように無地の形状の色を追加できます。

編集1

高密度デバイスの場合、低いディップ値を使用すると、ストロークや距離が非常に細くなったり、見えなくなったりすることがあります。これは、ListView ディバイダーを設定するときにも発生する可能性があります。

最も簡単な回避策は、1dp ではなく 1px の距離を使用することです。これにより、すべての密度で線が常に見えるようになります。最適な解決策は、密度ごとにディメンション リソースを作成して、各デバイスに最適なサイズを取得することです。

編集 2

楽しいですが、6 年後にこれを使用しようとしましたが、Lollipop デバイスでは良い結果が得られません。

おそらく、現在の解決策は 9 パッチを使用することです。長い間、Android はこの問題を簡単に解決する必要がありました。

于 2011-12-14T09:01:57.053 に答える
62

この質問はずっと前に投稿されたことを知っているので、この質問を投稿した人は回答を使用しませんが、他の人には役立つかもしれません.

 <?xml version="1.0" encoding="UTF-8"?>
    <!-- inset is used to remove border from top, it can remove border from any other side-->
    <inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:insetTop="-2dp"
        >
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rectangle">
        <stroke android:width="1dp" android:color="#b7b7b7" />
        <corners android:bottomRightRadius="5dp"  android:bottomLeftRadius="5dp"/>

        <solid android:color="#454444"/>
    </shape>
    </inset>

タグを使用して、inset削除するサイド ボーダーに負の値を指定します。

可能な値は次のとおりです。

android:insetTop="-1dp" android:insetBottom="-1dp" android:insetLeft="-1dp" android:insetRight="-1dp"

于 2015-08-21T17:05:04.313 に答える
42

リストレイヤーを使用して2つの形状を組み合わせることでこれを解決しました。そのうちの1つは高さ1dpで下部に配置されています

optionscreen_bottomrectangle.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
<item>
      <shape>
            <solid android:color="#535353" />
      </shape>
</item>
<!-- This is the main color -->
<item android:bottom="1dp">
     <shape>
           <solid android:color="#252525" />
     </shape>
</item>
</layer-list>

次に、layout/main.xml ファイルで

<TextView
    android:id="@+id/bottom_rectangle"
    android:background="@drawable/optionscreen_bottomrectangle"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_below="@id/table_options"
    android:layout_above="@id/exit_bar"/>

これは、table_options と exit_bar の間のギャップを背景で埋め、exit_bar が 1 dp 行を出力する直前です。これは私にとってはうまくいきました。他の誰かに役立つことを願っています。

レイヤーを正しい順序で配置するように編集された回答。ありがとうアレックス!

于 2010-03-30T16:04:39.440 に答える
26

レイヤーリストのドローアブルは上から下に描画されるため、@ Maraguesの答えは逆です(リストの最後のアイテムが上に描画されることを意味します):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
<item>
      <shape>
            <solid android:color="#535353" />
      </shape>
</item>
<!-- This is the main color -->
<item android:bottom="1dp">
     <shape>
           <solid android:color="#252525" />
     </shape>
</item>

</layer-list>

これにより、図形が線の色で効果的に塗りつぶされ、その上に背景色が描画され、線の色が透けて見えるように最後の 1 dp がクリアになります。

于 2011-09-12T21:40:16.120 に答える
2

次のコードを使用しました。

<?xml version="1.0" encoding="UTF-8"?>
<!-- inset is used to remove border from top, it can remove border from any other side-->
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetTop="-2dp" android:insetLeft="-2dp" android:insetRight="-2dp"
    >
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rectangle">
        <stroke android:width="1dp" android:color="@color/colorPrimary" />
        <solid android:color="#0000"/>
        <padding android:left="2dp" android:top="2dp" android:bottom="2dp" android:right="2dp"/>
    </shape>
</inset>
于 2016-03-25T06:45:59.240 に答える
0

シンプルで効率的

透明なビューを作成し、外側にあるすべてのものに何らかの色のオーバーレイを付けるには、外側のビューが中央のビューを踏むことなく、中央のビューをカプセル化するビューを構築することができます。

このようにして、レイアウトの外部にドローアブルを配置する必要がなくなり、実行時に計算を実行することなく、ビューの距離をより詳細に制御できます。

<androidx.constraintlayout.widget.ConstraintLayout .. >

    <View
        android:id="@+id/center"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:background="@color/transparent"/>

   <View
        android:id="@+id/left_overlay"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/center"
        app:layout_constraintTop_toBottomOf="@id/top_overlay"
        app:layout_constraintBottom_toTopOf="@id/bottom_overlay"
        android:background="@color/black"
        android:alpha="0.5"
        />

    <View
        android:id="@+id/right_overlay"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toRightOf="@id/center"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/top_overlay"
        app:layout_constraintBottom_toTopOf="@id/bottom_overlay"
        android:background="@color/black"
        android:alpha="0.5"
        />

    <View
        android:id="@+id/top_overlay"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/center"
        android:background="@color/black"
        android:alpha="0.5"
        />

    <View
        android:id="@+id/bottom_overlay"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/center"
        android:background="@color/black"
        android:alpha="0.5"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

GL

于 2020-09-08T21:56:00.513 に答える