简体   繁体   中英

How to create a See-Through View in Android XML-Layout

My Android XML-Layout looks like this

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:gravity="center"
          android:background="?android:colorBackground"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

        <SomeView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="@android:color/transparent"/>


    </LinearLayout>

What I need is a 'See-Through' hole in my Background LinearLayout at the size of my small View. Like in the following Picture:

在此处输入图片说明

Is this possible? How?

use transparent background in views that you want to make transparent so if there is any view behind it it will display

eg:

 android:background="#00000000"
  • Normal opaque black hex- "#000000"
  • Fully transparent - "#00000000"
  • Fully opaque - "#FF000000"
  • 50% transparent - "#80000000"

If you wish a see-through background for the view, what can be done is :-

  • Layout 1 : Take a parent layout filling the background as - android:layout_width="match_parent" android:layout_height="match_parent" : Say red color
  • Layout 2 : Now take another layout filling whatever size of the screen is needed : Say blue color
  • View : You can place your view [the see through one] in this layout and layout_width=20px, layout_height = 20px and background color should be the same as that of Layout1 that is red. This would now look like a see-through hole in the blue background.

Hope that this helps.

Example :

<LinearLayout
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent" android:background="@android:color/holo_red_dark">
    <LinearLayout
        android:orientation="vertical"
        android:gravity="center"
        android:background="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="30dp">

        <View
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@android:color/holo_red_dark"/>


    </LinearLayout>

</LinearLayout>

查找Alpha频道-我认为这基本上就是您在这里寻找的内容。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM