【问题标题】:Android Shape Line安卓形状线
【发布时间】:2010-05-03 11:34:06
【问题描述】:

我有以下代码:

   <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"/>

我有两个问题:

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

【问题讨论】:

    标签: android shape


    【解决方案1】:

    1)为了设置线条的颜色,你需要在&lt;stroke&gt;上定义android:color&lt;solid&gt; 元素用于背景。

    2) 您可以使用color values with an alpha layer,即#ARGB 设置形状的不透明度。在你的情况下,可能是#7FFF。

    【讨论】:

    • @Vaiden Android 支持 4 位 ARGB。在这种情况下,它将被解释为#77FFFFFF。
    【解决方案2】:

    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>
    

    使用 LinearLayout 和背景来显示线条。

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

    我希望这会有所帮助。

    【讨论】:

    • 如果我们使用&lt;View&gt;而不是LinearLayout,内存消耗会不会更少?
    • 是的,你可以使用它。由于每个类都从视图中扩展
    【解决方案3】:

    一个棘手的方法可能是这样的:

    1) 在你想要的地方添加一个视图。

    2) 将android:background 设置为可以设置颜色和不透明度的十六进制值。

    例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-31
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 2018-03-17
      • 1970-01-01
      相关资源
      最近更新 更多