0

Я хочу, чтобы достичь этого (лент над CardView): expectedAndroid: Отображение вида на ViewCard

А вот отрывок из моего решения:

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

И в Адаптер RecycleView Я привожу ленту вперед, вызывая.

itemView.findViewById(R.id.ribbonParent2Id).bringToFront(); 

Мое решение работает в большинстве устройств, которые я тестировал, кроме Nexus 5 (Android 5.1.1), где лента по-прежнему (частично) за cardview. Я использую AppCompat и таргетинг> v4.0.

  1. Каков правильный способ взглянуть на карту? Есть ли другой способ достичь того же? Почему мое решение не работает в некоторых случаях?
  2. Как я могу сделать зеленую ленту для начала с левого края экрана? В настоящее время все еще существует пространство между краем стяжки и началом ленты.

ответ

2

Это вызвано elevation, потому что CardView на андроид 21 использование истинный elevation и ваш LinearLayout имеет 0 высоту, так что за CardView.

Чтобы решить эту проблему, у вас есть 2 варианта

  1. набор Android: повышение на ваш LinearLayout, например, android:elevation="10px"
  2. обернуть CardView с FrameLayout, как показано ниже

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

+0

Я попытался что, тот же результат. CardView внутри RecycleView может обрабатываться несколько иначе. –

+0

Я проверил себя, это вызвано 'android: elevation' на android 21+, пожалуйста, проверьте мой обновленный ответ. –

+0

Так оно и было - спасибо. Теперь у меня странная тень (потому что лента не прямоугольная), но это другая проблема. –