【问题标题】:Set Cardview Shape & Background Semi transparent设置 Cardview 形状和背景半透明
【发布时间】:2018-11-23 23:21:45
【问题描述】:

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

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

【问题讨论】:

  • 请添加您的 .xml 文件
  • 添加了@Deadpool。

标签: android android-layout android-fragments


【解决方案1】:

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

您可以在颜色文件中创建一种具有透明度的颜色并将其设置为卡片背景。例如:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      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>
      

      【讨论】:

        【解决方案4】:

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

        编辑示例代码

        colors.xml

        <color name="semiTransparentColor">#20ffffff</color>
        

        并在 CardView 上设置此属性

        card_view:cardBackgroundColor="@color/semiTransparentColor"
        

        这会给你一张像玻璃一样的卡片。

        注意:你赋予颜色的不透明度百分比将是卡片的不透明度

        【讨论】:

          【解决方案5】:

          按照其他答案中所述设置 cardBackgroudColor 后,如果您在 cardview 周围出现一些阴影边框,请确保将高度设置为 0dp。

              app:cardBackgroundColor="@color/blue60"
              app:cardCornerRadius="20dp"
              app:cardElevation="0dp"
          

          【讨论】:

            猜你喜欢
            • 2020-07-21
            • 1970-01-01
            • 2018-12-20
            • 2015-11-12
            • 2013-07-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-02
            • 1970-01-01
            相关资源
            最近更新 更多