开发者

Android Shape Line

开发者 https://www.devze.com 2022-12-28 18:41 出处:网络
I have the following code: <shape xmlns:android=\"http://schemas.android.com/apk/res/android\" android:shape=\"line\">

I have the following code:

   <shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="line">
   <stroke android:width="1dp"/>
   <size android:height="1dp" />
   <solid  android:color="#FFF"/>
   </shape>


   <LinearLayout
     android:开发者_如何学JAVAlayout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background ="@drawable/line"/>

I have two questions:

  1. Why the line is black instead white? I have tried putting it inside a ImageView but the result is the same.
  2. How can i set the opacity of the shape?


1) In order to set the color of the line, you need to define android:color on the <stroke>. The <solid> element is for the background.

2) You can set the opacity of the shape by using color values with an alpha layer, aka #ARGB. In your case, maybe #7FFF.


The simplest example of the line for 2 density pixels with black color. Just save the xml in drawable folder: res/drawable/shape_black_line.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke 
    android:width="1dp" 
    android:color="#000000" />
<size android:height="2dp" />
</shape>

Use LinearLayout and background to show the line.

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:background="@drawable/divider">
</LinearLayout>

I hope this helped.


A tricky way can be like this:

1) Add a View where you want have a line.

2) set android:background to a hex value that you can set both color and opacity.

For example:

<View
    android:layout_width="match_parent"
    android:layout_height="6dp"
    android:background="#77f27123"
    android:orientation="vertical" />
0

精彩评论

暂无评论...
验证码 换一张
取 消