Android Shape Line

xge*_*86x 20 android shape

我有以下代码:

   <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:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background ="@drawable/line"/>
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

  1. 为什么线条是黑色而不是白色?我已经尝试将它放在ImageView中,但结果是一样的.
  2. 如何设置形状的不透明度?

Dan*_*Lew 26

1)为了设置线的颜色,你需要定义android:color<stroke>.该<solid>元素用于背景.

2)您可以使用带有alpha图层(也称为#ARGB)的颜色值来设置形状的不透明度.在你的情况下,也许#7FFF.


Hem*_*ori 15

具有黑色的2个密度像素的线的最简单示例.只需将xml保存在drawable文件夹中: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>
Run Code Online (Sandbox Code Playgroud)

使用LinearLayout和背景显示该行.

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:background="@drawable/divider">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助.

  • 如果我们使用`<View>`而不是LinearLayout,那不会减少内存消耗吗? (3认同)