2016-09-05 15 views
5

Moje urządzenie android jest 4.3 i nie działają okrągły róg cardView:Zaokrąglone karty narożneNie działają w systemie RecyclerView - Android?

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/tools" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/CardStart" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="5dp" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:scaleType="centerCrop" 
    app:cardUseCompatPadding="true" 
    card_view:cardBackgroundColor="@color/BlackTrans" 
    card_view:cardCornerRadius="5dp" 
    card_view:cardElevation="0dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <TextView 
       android:id="@+id/txtTitle" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 

      <TextView 
       android:id="@+id/txtDescription" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <ImageButton 
       android:id="@+id/imgbIcon" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:src="@drawable/ic_serch" /> 
     </LinearLayout> 
    </LinearLayout> 
</android.support.v7.widget.CardView> 

I napisać kod miech jest w mojej klasie, ale jeszcze nie działa:

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) 
{ 
    holder.CardStart.setCardElevation(0); 
    holder.CardStart.setBackgroundColor(ContextCompat.getColor(context,R.color.BlackTrans)); 
    holder.CardStart.setRadius(5); 
    holder.CardStart.setUseCompatPadding(true); 
} 
+0

Prawdopodobny duplikat [Promień narożnika CardView] (http://stackoverflow.com/questions/29342633/cardview-corner-radius) – TWL

+1

@ TWL. Czytam ten link. to nie moja odpowiedź. –

Odpowiedz

16

Okazuje się, że dzwoni View.setBackgroundColor(int) na CardView usuwa zaokrąglone rogi.

Aby zmienić kolor tła karty i zachować rogi, należy zamiast tego zadzwonić pod numer CardView.setCardBackgroundColor(int).

Może to dotyczyć niektórych użytkowników tego wpisu.


Kiedy rozszerzasz CardView, sugeruję dodanie następującej metody do ochrony narożników przed przypadkowym usunięciem:

/** 
* Override prevents {@link View#setBackgroundColor(int)} being called, 
* which removes the rounded corners. 
*/ 
@Override 
public void setBackgroundColor(@ColorInt int backgroundColor) { 
    setCardBackgroundColor(backgroundColor); 
} 

W szczególności pracował nad realizacją widok niestandardowy dla React Native i reagują było automatycznie stosuje kolor tła do widoku. To przesłonięcie rozwiązało ten problem; oznacza to, że inni deweloperzy nie muszą znać szczegółów danego widoku.

Powiązane problemy