【问题标题】:How to make a second color border with MaterialCardView?如何使用 MaterialCardView 制作第二个颜色边框?
【发布时间】:2021-11-25 06:07:14
【问题描述】:

我在网上找不到任何专门针对此问题提出的问题

Screenshot of Square Border

如何在图像中实现红色“第二”边框但在一侧?我使用 MaterialCardView 作为主要的外部边框。我是否可以在左侧制作一个带有红色边框的自定义形状并将其用作 MaterialCardView 的背景?

【问题讨论】:

    标签: java android xml android-studio materialcardview


    【解决方案1】:

    这可以通过三个嵌套的MaterialCardViews 轻松实现,只需更改每张卡的app:cardBackgroundColorapp:cardCornerRadiusandroid:layout_margin 属性。

    以下是 Xml 布局示例:

    <?xml version="1.0" encoding="utf-8"?>
    <com.google.android.material.card.MaterialCardView
        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="200dp"
        android:layout_margin="10dp"
        app:cardBackgroundColor="#81afdc"
        app:cardCornerRadius="10dp">
    
        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="4dp"
            app:cardBackgroundColor="#a70e09"
            app:cardCornerRadius="10dp">
    
            <com.google.android.material.card.MaterialCardView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="6dp"
                app:cardBackgroundColor="#ffffff"
                app:cardCornerRadius="6dp">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Sample Text"/>
    
            </com.google.android.material.card.MaterialCardView>
    
        </com.google.android.material.card.MaterialCardView>
    
    </com.google.android.material.card.MaterialCardView>
    

    结果:

    【讨论】:

      【解决方案2】:

      首先,创建两个可绘制资源文件,如下所示:

      bg_cardView_inner_color.xml

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
      
      <solid android:color="@android:color/holo_red_dark"/>
      
      <corners android:radius="7dp"/>
      
      </shape>
      

      bg_cardView_outline_color.xml

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
      
      
      <solid android:color="@color/white" />
      <stroke android:color="@android:color/holo_blue_light"
          android:width="5dp"/>
      <corners android:radius="10dp"/>
      </shape>
      

      然后,在您的 XML 文件中,添加这些文件,如下所示:

      activity_main.xml

      <?xml version="1.0" encoding="utf-8"?>
      <androidx.constraintlayout.widget.ConstraintLayout 
        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=".DemoActivity">
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:layout_marginStart="10dp"
          android:layout_marginEnd="10dp"
          android:layout_marginTop="10dp"
          android:layout_marginBottom="10dp"
          android:background="@drawable/bg_cardview_outline_color"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent">
      
          <androidx.cardview.widget.CardView
              android:layout_width="match_parent"
              android:layout_height="200dp"
              app:cardCornerRadius="5dp"
              app:cardElevation="2dp"
              app:cardUseCompatPadding="true"
              android:paddingBottom="5dp"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintTop_toTopOf="parent">
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="horizontal"
                  android:background="@drawable/bg_cardview_inner_color">
      
                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:background="@color/white"
                      android:gravity="center_vertical|center"
                      android:layout_marginStart="10dp">
                      <ImageView
                          android:layout_width="50dp"
                          android:layout_height="50dp"
                          android:src="@drawable/your_image"
                          app:tint="@color/black"
                          />
                      <TextView
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="your text"
                          android:textSize="30sp"
                          android:gravity="center"
                          android:textStyle="bold"
                          />
                  </LinearLayout>
      
              </LinearLayout>
          </androidx.cardview.widget.CardView>
      
      </LinearLayout>
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      【讨论】:

        猜你喜欢
        • 2017-08-11
        • 2019-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-16
        • 1970-01-01
        相关资源
        最近更新 更多