2017-04-14 58 views
4

我正在使用cardviews,但問題是我的CardView未顯示任何海拔或陰影。我已經嘗試了一些在stackoverflow中提示的方法,就像我嘗試使用海拔和陰影屬性一樣。我也試過使用card_view:cardUseCompatPadding =「true」,但沒有任何成功。這是我的xml文件CardView不顯示陰影高程

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    xmlns:card_view="http://schemas.android.com/tools" 
    android:layout_marginLeft="12dp" 
    android:layout_marginRight="12dp" 
    android:layout_marginTop="6dp" 
    android:layout_marginBottom="6dp" 
    app:cardBackgroundColor="#FAFBFD" 
    app:cardElevation="5dp"> 

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

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <de.hdodenhof.circleimageview.CircleImageView 
       android:id="@+id/post_profile_image" 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:src="@mipmap/ic_launcher" 
       android:layout_marginLeft="12dp" 
       android:layout_marginTop="12dp"/> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_toRightOf="@id/post_profile_image" 
       android:layout_marginLeft="12dp" 
       android:orientation="vertical" 
       android:layout_marginTop="16dp"> 

       <TextView 
        android:id="@+id/post_username" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Name" 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:textColor="#000000"/> 

       <TextView 
        android:id="@+id/post_time" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="2dp" 
        android:text="Time" 
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:textColor="@color/grayColor"/> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true"> 

       <ImageButton 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@android:drawable/ic_delete" 
        android:id="@+id/post_delete_btn" 
        android:visibility="invisible"/> 

      </LinearLayout> 

     </RelativeLayout> 

     <ImageView 
      android:id="@+id/post_image" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="12dp" 
      android:adjustViewBounds="true" 
      android:background="#00ffffff" 
      android:src="@drawable/add_btn" 
      android:visibility="gone"/> 

     <TextView 
      android:id="@+id/post_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="12dp" 
      android:layout_marginLeft="12dp" 
      android:text="Text about Posting...." 
      android:textAppearance="?android:attr/textAppearanceMedium"/> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="12dp" 
      android:layout_marginBottom="8dp"> 

      <ImageButton 
       android:id="@+id/like_btn" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="12dp" 
       android:background="#00ffffff" 
       android:src="@drawable/grey_like_bt"/> 

      <TextView 
       android:id="@+id/post_like_count" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="4dp" 
       android:layout_marginTop="8dp" 
       android:text="0 Likes" 
       android:textColor="#2196F3"/> 

      <ImageButton 
       android:id="@+id/comment_btn" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="32dp" 
       android:background="#00ffffff" 
       android:src="@drawable/comment_btn_gray"/> 

      <TextView 
       android:id="@+id/post_comment_count" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="4dp" 
       android:layout_marginTop="8dp" 
       android:text="0 Comments" 
       android:textColor="#2196F3"/> 

      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 

       <ImageButton 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@android:drawable/ic_menu_edit" 
        android:visibility="invisible" 
        android:id="@+id/post_edit_btn" 
        android:layout_alignParentRight="true"/> 

      </RelativeLayout> 

     </LinearLayout> 

    </LinearLayout> 

</android.support.v7.widget.CardView> 

不知道爲什麼它沒有顯示任何高度和陰影

+0

'cardElevation'在哪裏? –

+0

我已經使用了高程,但沒有給出任何結果,所以我已經刪除它 –

+0

邊距和陰影高程。 –

回答

0

你應該試試這個:

<android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     app:cardElevation="3dp"> 

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

     <!--Put your other fields between this--> 
</android.support.v7.widget.CardView> 
+0

嘗試過這個以及仍然沒有得到 –

0

使用這裏面cardView顯示陰影:

app:cardElevation="4dp" 
app:cardMaxElevation="4dp" 
+0

我已經使用這個Divyesh仍然沒有得到海拔在模擬器和手機 –

+0

添加應用程序:cardUseCompatPadding =「true」 –

+0

這也是Divyesh我已經指出了這個問題本身 –

0

試着把這個放在你的rootview中:

xmlns:card_view="http://schemas.android.com/apk/res-auto" 

,並使用此代碼爲海拔:

card_view:cardElevation="10dp" 
+0

已經使用這個哈里斯沒有得到任何結果 –

+0

嗯,我粘貼你的xml代碼,cardview在這裏顯示陰影。嘗試使用''無效緩存並重新啓動''在android工作室選項也許這可以幫助! –

-1
<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:cardBackgroundColor="@android:color/white" 
    android:foreground="?android:attr/selectableItemBackground" 
    android:layout_marginLeft="@dimen/activity_horizontal_margin" 
    android:layout_marginRight="@dimen/activity_horizontal_margin" 
    android:layout_marginTop="@dimen/padding_small" 
    android:layout_marginBottom="@dimen/padding_small" 
    app:cardCornerRadius="4dp" 
    app:cardElevation="4dp" > 

</android.support.v7.widget.CardView> 
+0

試過這個還沒有升高 –

0

在CardView小部件添加此行

app:cardElevation="5dp" 

希望這將幫助!

+0

沒有它還沒有工作 –

+0

首先刪除cardview的背景然後試試這個 – Akp

4
<android.support.v7.widget.CardView android:id="@+id/item_card" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    app:cardCornerRadius="2dp" 
    app:cardUseCompatPadding="true" 
    app:cardElevation="2dp" 
    app:cardBackgroundColor="@color/md_white_1000" 
    android:foreground="?android:attr/selectableItemBackground" 
    android:clickable="true"> 

</android.support.v7.widget.CardView> 
+1

app:cardUseCompatPadding =「true」發了一個招數) –

0

試試這個

<android.support.v7.widget.CardView 
      android:id="@+id/driverCurrentJobsCardView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/dim_8" 
      android:background="#ffffff" 
      app:cardCornerRadius="4dp" 
      card_view:cardElevation="4dp" 

>

,並確保您使用的是gradle這個這個依賴

compile 'com.android.support:cardview-v7:23.2.1' 
0
 <android.support.v7.widget.CardView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:cardElevation="10dp" 
    app:cardCornerRadius="4dp" 
    app:cardBackgroundColor="#ffff8800"> 
    ...... 
    </android.support.v7.widget.CardView> 

第一移除背景顏色,添加了海拔以後再添加背景。爲我工作,雖然我通過佈局設計師添加了背景。

1

答案是CardView及其父母的邊距和填充的組合。

嘗試這樣的:

<android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="@dimen/minimum_dimem" 
      android:layout_marginLeft="@dimen/default_margin" 
      android:layout_marginRight="@dimen/default_margin" 
      android:layout_marginTop="@dimen/minimum_dimem" 
      app:cardBackgroundColor="@color/white" 
      app:cardCornerRadius="@dimen/cardview_radius" 
      app:cardElevation="@dimen/cardview_default_elevation"> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="@dimen/ten_dimem"> 

       ... 

      </RelativeLayout> 
</android.support.v7.widget.CardView> 

而結果: enter image description here

1

如果您的清單文件有一個android:hardwareAccelerated="false"線,將其刪除。