2014-12-29 16 views
27

Tôi đang cố gắng sử dụng "android: elevation =" trong ứng dụng của mình nhưng một khi tôi chạy nó không xuất hiện trong thiết bị với Android 4.1.2"android: elevation =" không hoạt động trên các thiết bị trước Lollipop có biên dịch API21

gradle

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 21 
    buildToolsVersion "21.1.2" 

    defaultConfig { 
     applicationId "com.example.alvaro.proyectocaronte" 
     minSdkVersion 14 
     targetSdkVersion 21 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:21.0.3' 
} 

Layout.xml

<RelativeLayout 
       android:layout_width="1100dp" 
       android:layout_height="fill_parent" 
       android:background="@drawable/rounded_corner" 
       android:layout_alignParentTop="true" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentEnd="true" 
       android:layout_marginRight="93dp" 
       android:layout_marginEnd="93dp" 
       android:elevation="3dp"/> 

có lẽ tôi không biên dịch chính xác Lollipop cho các thiết bị trước kẹo, Bất kỳ đề xuất nào?

Nếu bạn cần để xem các phần khác của mã này, tôi sẽ chỉnh sửa câu hỏi

Cảm ơn

+0

nhận xét muộn .. Nhưng chắc chắn sẽ giúp cho bất cứ ai .. hãy thử điều này - http://stackoverflow.com/a/30962710/3879847 –

+1

Cảm ơn @RanjithKumar, chắc chắn sẽ giúp !! công việc tốt. – Aspicas

Trả lời

14

CẬP NHẬT ::

  1. Thực hành tốt nhất để làm điều đó là

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

    và thêm thư viện cho cardview

    dependencies { 
        ... 
        compile 'com.android.support:cardview-v7:21.0.+' 
    } 
    
  2. On Pre-Lollipop bạn có thể sử dụng drawable

    android: background = "@ android: drawable/dialog_holo_light_frame"

    nó sẽ cung cấp cho bạn nhìn của cao

  3. bạn có thể tạo của riêng bạn như thế này

    <?xml version="1.0" encoding="utf-8"?> 
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    
    <item> 
    <shape android:shape="rectangle"> 
        <solid android:color="#BDBDBD"/> 
        <corners android:radius="5dp"/> 
    </shape> 
    </item> 
    
    <item 
    android:left="0dp" 
    android:right="0dp" 
    android:top="0dp" 
    android:bottom="2dp"> 
    <shape android:shape="rectangle"> 
        <solid android:color="#ffffff"/> 
        <corners android:radius="5dp"/> 
    </shape> 
    </item> 
    </layer-list> 
    

reference

6

Bạn cũng có thể sử dụng một CardView từ thư viện hỗ trợ để thực hiện các bề mặt.
Để làm như vậy, hãy thêm phụ thuộc vào bản dựng của bạn.gradle:

compile 'com.android.support:cardview-v7:23.1.1' 

Và sau đó chỉ cần sử dụng nó trong bố trí của bạn:

<android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="150dp" 
     android:layout_margin="16dp" 
     android:background="#fff" 
     > 
    </android.support.v7.widget.CardView> 

Ở đây bạn có khá nhiều lựa chọn để tùy chỉnh nó so với sử dụng @android:drawable/dialog_holo_light_frame làm nền

EDIT:
Cũng lưu ý rằng, phương pháp này cho phép bạn chỉ cần triển khai
Ma Thiết kế terial trên thiết bị Pre-Lolipop.
Bạn có thể thay đổi độ cao,
tròn các góc, vv
Để làm như vậy, bạn phải:

app:cardElevation="8dp" 
app:cardCornerRadius="8dp" 
app:contentPadding="5dp"> 

Và không quên thêm xmlns:app="http://schemas.android.com/apk/res-auto" để bố trí gốc.

Ngoài ra bạn có thể dễ dàng thay đổi độ cao trong mã của bạn:

CardView card = (CardView) findViewById(R.id.yourPreetyCoolCardView); 
card.setCardElevation(getResources() 
    .getDimension(R.dimen.card_picked_up_elevation)); 

Sử dụng 8dp cho nhặt và 2dp cho nghỉ ngơi (bình thường) nhà nước và bạn sẽ tuyệt vời.

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