【问题标题】:android highlight cardview borderandroid高亮cardview边框
【发布时间】:2019-03-08 03:37:54
【问题描述】:

我在回收视图中使用卡片,并想突出显示其中的一些。

我的第一个想法是颜色鲜艳的不同阴影或边框,但到目前为止我找不到任何可以调整它的属性

这是我的卡片视图:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="@dimen/giveawayCardHeight"
    android:layout_gravity="center"

    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="3dp"
    card_view:cardUseCompatPadding="true"
    >

    <android.support.v7.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="5dp">

        <RelativeLayout
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:background="#FFFFFF">

            <android.support.v7.widget.AppCompatImageView
                android:id="@+id/raffleThumbnail"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                 />

            <com.tomatedigital.giveawaymaster.ui.FontFitTextView
                android:id="@+id/raffleOwner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:layout_alignParentBottom="true"
                android:alpha="0.7"
                android:background="@color/colorPrimaryDark"
                android:gravity="center_horizontal|bottom"
                android:text="viajandocomgabi"
                android:textColor="@color/cardview_light_background"
                android:textSize="@dimen/normalFont" />
        </RelativeLayout>

        <android.support.v7.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="@dimen/smallMargin"
            android:orientation="vertical"
            android:background="#FFFFFF">

            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/raffleDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"

                android:text="@string/raf_date_label"
                android:textSize="@dimen/normalFont" />


            <android.support.v7.widget.LinearLayoutCompat
                android:id="@+id/giveawayButtonsLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">
                <Space
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />

                <android.support.v7.widget.AppCompatImageButton
                    android:id="@+id/startButton"
                    android:layout_width="@dimen/cardButtonSize"
                    android:layout_height="@dimen/cardButtonSize"


                    android:background="?android:attr/selectableItemBackground"
                    android:scaleType="fitXY"
                    android:src="@drawable/ic_play" />

                <Space
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2" />

                <android.support.v7.widget.AppCompatImageButton
                    android:id="@+id/deleteButton"
                    android:layout_width="@dimen/cardButtonSize"
                    android:layout_height="@dimen/cardButtonSize"

                    android:background="?android:attr/selectableItemBackground"
                    android:scaleType="fitXY"
                    android:src="@drawable/ic_trash" />

                <Space
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />
            </android.support.v7.widget.LinearLayoutCompat>

            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/ticketAmountLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"

                android:drawableRight="@drawable/ic_tickets"
                android:text="@string/you_have"
                android:textSize="@dimen/normalFont" />
        </android.support.v7.widget.LinearLayoutCompat>
    </android.support.v7.widget.LinearLayoutCompat>
</android.support.v7.widget.CardView>

这就是我想要的样子:

最好的方案是将默认阴影颜色切换为更亮的颜色,但如果不可能,则可以使用内边框

我该怎么做?

【问题讨论】:

    标签: android android-recyclerview android-cardview android-shape


    【解决方案1】:

    这有点难看,但您可以在模型上添加另一个字段,用作在卡片视图上设置不同背景的基础。然后在适配器上,根据它的状态以编程方式更改 cardview 的背景。如果需要,您需要经常通知DatasetChanged。

    【讨论】:

      【解决方案2】:

      试试这个库,太棒了

      安装

      dependencies {
          implementation 'com.loopeer.lib:shadow:0.0.4-beta3' 
      }
      

      用法

      <com.loopeer.shadow.ShadowView
          android:id="@+id/shadow_view"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="12dp"
          android:elevation="10dp"
          android:foreground="?attr/selectableItemBackground"
          android:onClick="onShadowClickTest"
          android:padding="10dp"
          app:cornerRadius="4dp"
          app:shadowMargin="20dp"
          app:shadowRadius="14dp">
      
      <TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="YOUR_TEXT"/>
      </com.loopeer.shadow.ShadowView>
      

      属性

      android:foreground  
      shadowMargin  
      shadowMarginTop  
      shadowMarginLeft  
      shadowMarginRight  
      shadowMarginBottom  
      cornerRadius  
      cornerRadiusTL  
      cornerRadiusTR  
      cornerRadiusBL  
      cornerRadiusBR  
      foregroundColor  
      shadowColor  
      shadowDx  
      shadowDy  
      shadowRadius  
      backgroundColor
      

      希望对你有帮助

      【讨论】:

        【解决方案3】:

        对于内边框,您可以像这样创建一个xml drawable 文件

        <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="4dp" />
            <stroke android:color="@color/colorAccent" android:width="8dp" />
        </shape>
        

        然后将其作为CardViews 第一个孩子的background

        cardCorner radiusxmlbackgroundcorner radius设置为相同。

        【讨论】:

          猜你喜欢
          • 2020-07-04
          • 1970-01-01
          • 1970-01-01
          • 2017-12-11
          • 2020-08-23
          • 2020-06-25
          • 1970-01-01
          • 1970-01-01
          • 2015-10-15
          相关资源
          最近更新 更多