2016-09-05 25 views
5

thiết bị Android của tôi là 4.3 và không có tác dụng góc tròn của cardView:Thẻ góc trònViewView không hoạt động trong 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> 

Và tôi viết mã dưới đây trong lớp học của tôi nhưng không làm việc được nêu ra:

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

Bản sao có thể có của [Bán kính góc CardView] (http://stackoverflow.com/questions/29342633/cardview-corner-radius) – TWL

+1

@ TWL.I đọc liên kết này. nó không phải là phản ứng của tôi. –

Trả lời

16

Nó chỉ ra rằng gọi View.setBackgroundColor(int) trên CardView loại bỏ các góc tròn.

Để thay đổi màu nền của thẻ và giữ lại các góc, bạn cần gọi số CardView.setCardBackgroundColor(int) để thay thế.

Đó có thể là trường hợp đối với một số khách truy cập vào bài đăng này.


Khi subclassing CardView, tôi khuyên bạn nên bổ sung thêm các phương pháp sau đây để bảo vệ các góc của bạn từ bất ngờ loại bỏ:

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

Trong đó, tôi đã làm việc trên một cài đặt giao diện tùy chỉnh cho Phản ứng Native, và phản ứng là tự động áp dụng màu nền cho chế độ xem. Ghi đè này đã giải quyết được vấn đề đó; điều đó có nghĩa là các nhà phát triển khác không cần biết chi tiết về chế độ xem cơ bản.

Các vấn đề liên quan