Android 安卓形状线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2757707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:14:48  来源:igfitidea点击:

Android Shape Line

androidshape

提问by xger86x

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:layout_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. 为什么这条线是黑色而不是白色?我试过把它放在 ImageView 中,但结果是一样的。
  2. 如何设置形状的不透明度?

回答by Dan Lew

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

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

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

2) 您可以通过使用颜色值与 alpha 层(又名 #ARGB )来设置形状的不透明度。在你的情况下,也许#7FFF。

回答by Hemant Shori

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

黑色的 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>

Use LinearLayout and background to show the line.

使用 LinearLayout 和 background 来显示线条。

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

I hope this helped.

我希望这有帮助。

回答by Kaaveh Mohamedi

A tricky way can be like this:

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

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

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

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

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

For example:

例如:

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