【问题标题】:how i i can transparent background on android studio?我怎样才能在android studio上透明背景?
【发布时间】:2017-12-11 11:00:49
【问题描述】:

我想设计一个与下图完全相同的布局。

当我将线性布局 alpha 设置为透明时,其中的所有图标也变得透明并且完全正常。但我想要一种方法让透明背景不是所有项目!

我想要这样的透明布局:

但我的布局显示如下:

这是我的代码:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:background="@color/colorPrimary"
    android:orientation="vertical"
    android:alpha="0.5"
    >



    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="1"
        >

        <RelativeLayout
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_weight="0.333"
            android:padding="10dp"
            >

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@mipmap/consert"
                android:layout_gravity="center_horizontal"
                />

        </RelativeLayout>
        <RelativeLayout
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_weight="0.333"
            android:padding="10dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@mipmap/consert"
                android:layout_gravity="center_horizontal"
                />

        </RelativeLayout>
        <RelativeLayout
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_weight="0.333"
            android:padding="10dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@mipmap/consert"
                android:layout_gravity="center_horizontal"
                />

        </RelativeLayout>
    </LinearLayout>



    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <RelativeLayout
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_weight="0.333">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@mipmap/consert"
                android:layout_gravity="center_horizontal"
                />

        </RelativeLayout>
        <RelativeLayout
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_weight="0.333">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@mipmap/consert"
                android:layout_gravity="center_horizontal"
                />

        </RelativeLayout>
        <RelativeLayout
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_weight="0.333">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@mipmap/consert"
                android:layout_gravity="center_horizontal"
                />

        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 用透明布局制作背景,然后在透明部分设置你的图标。你的结果会让你开心
  • 我认为这就是我所做的!你能告诉我如何在我的代码中修复它吗? tnx
  • 在顶部的LinearLayout上,给它一个透明的背景颜色,比如说做一个#FF000000,这将防止你的布局小部件受到alpha属性的影响。
  • @Saeid 不,你没有那样做。
  • 旁注:您正在使用不必要的布局。而且布局嵌套不利于性能。

标签: android layout transparent alpha


【解决方案1】:

如果你想调整透明,你可以尝试这样改变我给的颜色的 alpha #55000000 你可以随意改变 alpha 值(55)。如果你想要黑色透明,你可以使用#55FFFFFF

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


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:background="#55000000"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="3"
            >

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:padding="10dp"
                >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:background="@mipmap/ic_launcher"
                    />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:padding="10dp"
                >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:background="@mipmap/ic_launcher"
                    />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:padding="10dp"
                >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:background="@mipmap/ic_launcher"
                    />

            </RelativeLayout>
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="3"
            >

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:padding="10dp"
                >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:background="@mipmap/ic_launcher"
                    />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:padding="10dp"
                >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:background="@mipmap/ic_launcher"
                    />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:padding="10dp"
                >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:background="@mipmap/ic_launcher"
                    />

            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

【讨论】:

    【解决方案2】:

    为了透明

    android:background="00000000";
    

    黑色透明(在你的情况下)

    android:background="55000000";
    

    【讨论】:

      【解决方案3】:

      这个透明度代码也对我有用:

      android:background="@null"
      

      另一方面,对于黑色透明度(正如其他人之前回答的那样)

      android:background="55000000"
      

      【讨论】:

        猜你喜欢
        • 2020-10-29
        • 2017-09-02
        • 1970-01-01
        • 2018-08-16
        • 2019-12-26
        • 2017-09-02
        • 1970-01-01
        • 2017-08-12
        • 1970-01-01
        相关资源
        最近更新 更多