"LinearLayout"中的白色边框和透明度问题

21

我想添加一个线性布局,具有透明背景和白色边框。问题是:就我所查询到的,我只能实现其中一个。

这是我做的:

  1. 将以下内容保存为border.xml在drawable中

      <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item> 
           <shape android:shape="rectangle">
           <solid android:color="#FFFFFF" /> 
      </shape>
      </item>   
         <item android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp" >  
         <shape android:shape="rectangle"> 
         </shape>
       </item>    
      </layer-list> 
    
  2. 我的现有页面布局

  3.      <LinearLayout
            android:id="@+id/quiz"
            android:layout_width="150dp"
            android:layout_height="120dp"
            android:background="#66041414"          <-------- replaced it with android:background="@drawable/border"
            android:orientation="vertical"
            android:layout_marginLeft="5dp" >
    
             ......
           </LinearLayout>
    

我在加入边框时,背景变得不透明了。

我希望最终效果如下:


参考图片

我对此感到非常困惑。只是想找到一种方法来实现它。任何建议都将非常有帮助。


我在这里回答了,使用xml的方式会有所帮助。 - RobinHood
@Anurag:请查看下面的答案更新,附带证明。 - VendettaDroid
4个回答

45

背景布局的可绘制对象:

如果您愿意,可以更改角落形状的半径。但是描边将创建一个边框,实心部分是我们正在使透明的背景。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
  <corners
      android:radius="2dp"
      android:topRightRadius="0dp"
      android:bottomRightRadius="0dp"
      android:bottomLeftRadius="0dp" />
  <stroke
      android:width="1dp"
      android:color="@android:color/white" />
  <solid android:color="@android:color/transparent"/>
</shape>

和我的测试布局.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <LinearLayout 
        android:id="@+id/ll2"
        android:layout_height="50dp"
        android:layout_width="50dp"
        android:background="@drawable/my_transparent_linear_layout"></LinearLayout>
</LinearLayout>

它有效,以下是证明:

enter image description here


1
确实有效...我知道我的话不足以表达我的感激,但还是非常感谢你,伙计...你让我的一天变得美好 :) :) - anurag
1
谢谢,老兄。运行得像个魅力一样。 - TacoEater
底部边框怎么样? - azurh

2

背景的Xml Drawable:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="30dp" />
<stroke android:width="5dp" android:color="#ffffffff"/>
<solid android:color="#66000000"/>
</shape>

根据您的需求调整半径、宽度和深色透明度(#ff和#66部分)。


我只能为android:background设置一个值...它要么是边框,要么是透明度...根据您的代码,我得到了边框,但没有透明度。我希望你理解我的意思?无论如何,感谢您提供优化后的边框代码。 - anurag
@anurag 只需编辑颜色,前两位数字用于透明度(#00 到 #ff)。我已将默认填充颜色设置为 #66000000,即半透明黑色,并将默认边框颜色设置为 "#ffffffff",即完全不透明的白色。 - S.D.
@anurag。颜色值#FFBBCCDD - FF -> 透明度级别(00为最透明),BB -> 红色级别(FF -> 完全红色),CC -> 绿色级别,DD -> 蓝色级别。如果颜色值只有6个十六进制字符而不是8个,那么透明度将被忽略(元素是不透明的)。#FFCCBBDD等于#CCBBDD。下次至少尝试一下答案,因为@Singularity在图像中做了你要求的事情。 - cosmincalistru

2

确实是@Ali Imran提供的好建议,可以尝试以下方法,希望能有所帮助。

back.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
         <stroke android:width="1dp" android:color="#dd7b7a"/>
         <corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp" 
         android:topLeftRadius="10dp" android:topRightRadius="10dp"/> 
         <solid android:color="#dd7b7a"/>
     </shape>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    >
<LinearLayout 
     android:padding="4dip"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/back"
    android:gravity="center_horizontal"
    >
<LinearLayout  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
   android:background="@drawable/tile_mode" // your transparent image
    />
</LinearLayout>  
</LinearLayout>

同时您可以查看以下链接,以此方法使用xml能让您更好地理解。

带描边的圆角位图图像


先生,感谢您的帮助,非常感激... 顺便说一句,我从 @VendettaDroid 那里得到了我需要的东西... - anurag

2

您可以使用两个布局对齐,一个在另一个上面,然后将顶部视图的背景设置为透明,将白色边框设置为底部视图的背景。 您可以在相对布局中完成此操作。


尝试使用@VendettaDroid的解决方案。 - Ali Imran

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接