[英]Set Cardview Shape & Background Semi transparent
我在线性布局和卡片视图中添加了背景图像。 在 CardView 下,我添加了另一个渐变背景图像并设置了 alpha .5。 当我添加 TextView 并添加 Text 时,文本也变得半透明,如图所示。 我尝试使用 Bold 外观,但没有用。 我已上传图片
<?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_marginTop="0dp"
android:layout_width="match_parent"
android:layout_height="215dp"
android:src="@drawable/ic_format_quote_black_24dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_margin="10dp"
android:layout_width="350dp"
android:layout_height="70dp"
app:cardCornerRadius="40dp"
android:alpha=".5">
<LinearLayout
android:layout_margin="6dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rectangle"
android:alpha=".5">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill"
android:text="Don’t let a mad world tell you that success is anything other than a successful present moment. "
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="13dp"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
通过将 cardview 的 alpha 设置为 0.5,您使其所有后代视图的 alpha 也为 0.5。 您应该使linearlayout 的背景即@drawable/rectangle 具有带alpha 的颜色( ARGB,例如#98000000),而不是将0.5 的alpha 设置为linearlayout 和cardview。
您可以为十六进制颜色提供透明度。 每个透明度百分比都有一个唯一的代码,正如这里提到的更多详细信息。
您可以在颜色文件中创建一种具有透明度的颜色并将其设置为卡片背景。 例如:
<color name="semiTransparentColor">#40FFFFFF</color>
表示白色的不透明度为 25%。
TextView 的不透明度已更改,因为它是 CardView 的子项。 通过将 TextView 和 CardView 分别放置在启用重叠然后居中的布局中,将其放置在 CardView 上方。 通过这种方式,您可以为视图设置不同的 alpha。 试试下面的 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 的背景。
编辑示例代码
<color name="semiTransparentColor">#20ffffff</color>
并在 CardView 上设置此属性
card_view:cardBackgroundColor="@color/semiTransparentColor"
这会给你一个像卡片一样的玻璃。
注意:您赋予颜色的不透明度百分比将是卡片的不透明度
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.