java 白色边框以及“LinearLayout”中的透明度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14535644/
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-10-31 16:34:57  来源:igfitidea点击:

White Border along with transparency in "LinearLayout"

javaandroidbordertransparencyandroid-drawable

提问by anurag

I wanted to add a linear layout, having a transparent background as well as with white borders. The problem is: as far as I have googled, I can achieve only one out of both.

我想添加一个线性布局,具有透明背景和白色边框。问题是:据我用谷歌搜索,我只能实现两者中的一个。

Here's what I did:

这是我所做的:

  1. Saved the following as border.xml in 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. my existing page layout

         <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>
    
  1. 在drawable中将以下内容保存为border.xml

      <?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. 我现有的页面布局

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

I am getting opaque background, when the border was included.

当包含边框时,我的背景变得不透明。

I wanted a final result to be like:


Reference image

我希望最终结果是这样的:


参考图片

totally stuck with it. Just wanted to find a way out to achieve it. Any suggestions would be quite helpful.

完全坚持下去。只是想找到一种方法来实现它。任何建议都会很有帮助。

回答by VendettaDroid

Your drawable for background of layout:

您用于布局背景的可绘制对象:

You can change radius for corner shape if you want. But stroke will create a border and solid part is the background which we are making transparent.

如果需要,您可以更改角形状的半径。但是笔触会创建一个边框,实心部分是我们要透明的背景。

<?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>

and my test layout.xml

和我的测试 layout.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>

It works, Below is the proof:

它有效,以下是证明:

enter image description here

在此处输入图片说明

回答by S.D.

Xml Drawable for background:

用于背景的 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>

Adjust radius, width and dark color transparency ( #ff and #66 parts) as you please.

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

回答by Ali Imran

For this you can use two layout alignedone top of the other then set the background transparentfor the top viewand set the white border as background for the bottom view. You can do this thing inside relative layouts.

为此,您可以用两个版面aligned的其他的一个顶部则设置背景transparenttop view,并设置白色边框作为背景的bottom view。你可以在里面做这件事relative layouts

回答by RobinHood

Indeed well suggestion by @Ali Imran, check below way, hope it will help.

@Ali Imran 的建议确实很好,请检查以下方式,希望它会有所帮助。

back.xml

返回.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>

also go through below links in that, way of using xmlwill works for you.

也通过下面的链接,使用方式xml对你有用。

Bitmap image with rounded corners with stroke

带有笔划的圆角位图图像