Android 设置Cardview形状&;背景半透明

Android 设置Cardview形状&;背景半透明,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,我在线性布局和卡片视图中添加了背景图像。在CardView下,我添加了另一个渐变背景图像并设置alpha.5。当我添加TextView和添加文本时,文本也变得半透明,如图所示。我试着用粗体的外观,但没用。 通过将cardview的alpha设置为0.5,您将使其所有子视图的alpha也为0.5。您应该使linearlayout的背景,即@drawable/rectangle具有alpha(ARGB例如#98000000)的颜色,而不是将alpha设置为0.5以设置linearlayout和c

我在线性布局和卡片视图中添加了背景图像。在CardView下,我添加了另一个渐变背景图像并设置alpha.5。当我添加TextView和添加文本时,文本也变得半透明,如图所示。我试着用粗体的外观,但没用。



通过将cardview的alpha设置为0.5,您将使其所有子视图的alpha也为0.5。您应该使linearlayout的背景,即@drawable/rectangle具有alpha(ARGB例如#98000000)的颜色,而不是将alpha设置为0.5以设置linearlayout和cardview。

文本视图的不透明度更改,因为它是cardview的子对象。通过将TextView和CardView分别放置在允许重叠的布局中,然后将它们居中,将其放置在CardView上方。这样,您可以为视图设置不同的字母。尝试下面的XML

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.vikramgehlot.QuotesandStatus.Apj"
        android:background="@drawable/materialbackground" >
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="215dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="215dp"
                    android:layout_marginTop="0dp"
                    android:src="@drawable/ic_format_quote_black_24dp" />

                <RelativeLayout
                    android:layout_width="350dp"
                    android:layout_height="70dp"
                    android:alpha="1">

                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <android.support.v7.widget.CardView
                            android:layout_width="350dp"
                            android:layout_height="70dp"
                            android:alpha="1"
                            android:visibility="visible"
                            app:cardCornerRadius="40dp">

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_margin="6dp"
                                android:alpha=".5"
                                android:background="@drawable/rectangle">

                            </LinearLayout>
                        </android.support.v7.widget.CardView>
                    </FrameLayout>

                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:alpha="1"
                            android:gravity="fill"
                            android:text="Don’t let a mad world tell you that success is anything other than a successful present moment. "
                            android:textAllCaps="false"
                            android:textColor="@color/black"
                            android:textSize="13dp"
                            android:textStyle="bold" />
                    </FrameLayout>

                </RelativeLayout>

            </LinearLayout>
        </ScrollView>
    </LinearLayout>

只需在colors.xml中定义一种低不透明度的颜色,并将其作为cardview的背景

编辑示例代码

colors.xml 这会给你一张玻璃样的卡片


注意:您为颜色提供的不透明度百分比将是卡的不透明度

您可以为十六进制颜色提供透明度。每个百分比的透明度都有一个唯一的代码,如更多详细信息所述

您只需在颜色文件中创建具有透明度的颜色,并将其设置为卡背景。例如:

<color name="semiTransparentColor">#40FFFFFF</color>
#40FFFFFF

白色表示25%的不透明度。

请添加您的.xmlfile@Deadpool补充。
<color name="semiTransparentColor">#20ffffff</color>
card_view:cardBackgroundColor="@color/semiTransparentColor"
<color name="semiTransparentColor">#40FFFFFF</color>