Android:在ViewCard上显示视图

Android:在ViewCard上显示视图,android,android-layout,android-5.0-lollipop,android-recyclerview,android-cardview,Android,Android Layout,Android 5.0 Lollipop,Android Recyclerview,Android Cardview,我想实现这一点(CardView上的功能区): 下面是我的解决方案的片段: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/testId" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >

我想实现这一点(CardView上的功能区):

下面是我的解决方案的片段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/ribbonParentId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|top"
    android:layout_marginBottom="15dp"
    android:background="@drawable/green_ribbon" >

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/ribbonId"
        style="@style/RibbonStyle"
        android:text="@string/ribbon_text" />
</LinearLayout>

<android.support.v7.widget.CardView
    android:id="@+id/historicalWeightCardViewId"
    style="@style/MyCardViewStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="18dp" >

          ... Other elements

</android.support.v7.widget.CardView>
除了Nexus5(Android 5.1.1)之外,我的解决方案在我测试的大多数设备上都能正常工作,ribbon仍然(部分)位于cardview后面。我正在使用AppCompat和targeting>v4.0

  • 将视图置于cardview之上的正确方式是什么?有没有其他方法可以达到同样的效果?为什么我的解决方案在某些情况下不起作用
  • 如何使绿色丝带从屏幕的左边缘开始?当前,熨平板边缘和功能区起点之间仍有一个空间

  • 这是因为android 21上的CardView使用true
    elevation
    ,而您的LinearLayout的标高为0,因此它位于CardView的后面

    要解决此问题,您有两种选择

  • 将android:elevation设置为您的
    线性布局
    ,例如
    android:elevation=“10px”
  • 用如下所示的
    FrameLayout
    包装您的
    CardView

    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <android.support.v7.widget.CardView
                android:id="@+id/historicalWeightCardViewId"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_marginTop="18dp" >
    
        </android.support.v7.widget.CardView>
    </FrameLayout>
    
    <LinearLayout
            android:id="@+id/ribbonParentId"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_gravity="left|top"
            android:layout_marginBottom="15dp"
            android:background="@android:color/holo_red_dark">
    
        <android.support.v7.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ribbonId"
                android:text="abc" />
    </LinearLayout>
    

    
    


  • 我试过了,同样的结果。RecycleView中的CardView可能处理方式有所不同。我已经检查过自己,这是由android 21+上的
    android:elevation
    引起的,请检查我的更新答案。就是这样-谢谢。现在我有一个奇怪的阴影(因为缎带不是矩形的),但这是不同的问题。
    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <android.support.v7.widget.CardView
                android:id="@+id/historicalWeightCardViewId"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_marginTop="18dp" >
    
        </android.support.v7.widget.CardView>
    </FrameLayout>
    
    <LinearLayout
            android:id="@+id/ribbonParentId"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_gravity="left|top"
            android:layout_marginBottom="15dp"
            android:background="@android:color/holo_red_dark">
    
        <android.support.v7.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ribbonId"
                android:text="abc" />
    </LinearLayout>