2016-09-05 8 views
5

私のAndroidデバイスが4.3あるとcardViewのラウンドコーナーを動作しません:丸みを帯びたコーナーcardViewは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> 

そして、私は私のクラスで怒鳴るコードを書くが、まだ動作しません。

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

[CardView Corner Radius](http://stackoverflow.com/questions/29342633/cardview-corner-radius)の可能な複製 – TWL

+1

@ TWL.Iこのリンクを読んでください。それは私の反応ではありません。 –

答えて

16

結局のところ(第2への参照が含まれている)最初の属性のドキュメント https://developer.android.com/reference/android/support/v7/widget/CardView.html#setPreventCornerOverlap(boolean)

これはトリックを行う必要があることCardViewView.setBackgroundColor(int)を呼び出します丸い角を取り除きます。

カードの背景色を変更してコーナーを保持するには、代わりにCardView.setCardBackgroundColor(int)に電話する必要があります。

この投稿への特定の訪問者の場合があります。


、私は誤って削除からあなたのコーナーを保護するために、次のメソッドを追加することをお勧めCardViewをサブクラス:だった、私はネイティブに反応のためのカスタムビューの実装に取り​​組んでいた。特に

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

、して反応ビューに背景色を自動的に適用します。このオーバーライドはその問題を解決しました。これは、他の開発者が基になるビューの詳細を知る必要がないことを意味します。

2

この2つの属性をあなたのカードビューここ

card_view:cardPreventCornerOverlap="false" 
card_view:cardUseCompatPadding="true" 

関連する問題