有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我想添加一个线性布局,有一个透明的背景以及白色边框。问题是:就我在谷歌搜索的结果而言,两者中我只能实现一个

以下是我所做的:

  1. 将以下内容保存为边框。可绘制的xml

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

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

我得到不透明的背景,当边界被包括在内

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


Reference image

完全被它卡住了。我只是想找到一个办法来实现它。任何建议都会很有帮助


共 (4) 个答案

  1. # 1 楼答案

    您的可绘制布局背景:

    如果需要,可以更改拐角形状的半径。但是笔划将创建一个边框,而实体部分是我们正在使其透明的背景

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

  2. # 2 楼答案

    的确,@Ali Imran的建议很好,请检查下面的方法,希望它能有所帮助

    回来。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的方式对你来说就行了

    Bitmap image with rounded corners with stroke

  3. # 3 楼答案

    为此,您可以使用两个布局aligned,一个顶部另一个顶部,然后为top view设置背景transparent,并将白色边框设置为bottom view的背景。您可以在relative layouts内执行此操作

  4. # 4 楼答案

    可用于后台的Xml绘图:

    <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部分)