2014-07-11 8 views
7

मैं कुछ दिनों के लिए एल पूर्वावलोकन के साथ खेल रहा है, और एक बात यह है कि काम करने के लिए प्रतीत नहीं होता है एक दृश्य के लिए android:elevation विशेषता है। View.setElevation() पर कॉल करते समय यह ठीक काम करता है, लेकिन ऐसा लगता है कि यह XML विशेषता को अनदेखा करता है। मैं एंड्रॉयड स्टूडियो 0.8.2 का उपयोग कर रहा है और minSdkVersion और 'L' करने के लिए targetSdkVersion सेट, और 'android-L' करने के लिए compileSdkVersion सेट है। buildToolsVersion"20.0.0" है।एंड्रॉयड: ऊंचाई एल पूर्वावलोकन में काम नहीं करता है

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_margin="10dp" 
     android:elevation="5dp"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="top"/> 

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

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_margin="10dp" 
     android:elevation="0dp"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="bottom"/> 

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

</LinearLayout> 

यहाँ परिणाम का एक स्क्रीनशॉट है:: यहाँ एक नमूना लेआउट कि ठीक से काम नहीं करता है imgur.com/mqeq9vK

android:elevation मैं सेट के अनुसार, नीचे कार्ड फ्लैट पर होना चाहिए "जमीन", लेकिन यह नहीं है। वास्तव में, यह कोई शीर्ष कार्ड जो ऊंचाई 5dp है से अलग दिखता है। क्या यह एक ज्ञात मुद्दा है, और क्या यह आपके लिए काम करता है?

संपादित करें: ऐसा लगता है कि यह केवल CardView के साथ मामला है, या कम से कम यह Buttons के साथ कोई समस्या नहीं है। इस लेआउट में मैं Button साथ CardView बदल दिया, और भले ही छाया तरह का बँधा हुआ है (ज्ञात बग), तब भी आप ऊंचाई में अंतर देख सकते हैं।

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:elevation="2dp"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:elevation="1dp"/> 

</LinearLayout> 

संपादित करें 2: यह बहुत ज्यादा पुष्टि की है कि इस मुद्दे को केवल CardView, android:elevation किसी अन्य दृश्य के लिए ठीक काम करता है को प्रभावित करता है है। यह क्यों होता है इसका एक विस्तृत स्पष्टीकरण के लिए स्वीकृत उत्तर को देखें। इस बीच में, मेरी वैकल्पिक हल एक FrameLayout साथ CardView की जगह और एक drawable को android:background स्थापित करने के लिए है।

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <solid android:color="#fff" /> 
</shape> 

मैं भी एक बग के रूप में यह मामला दर्ज किया है, तो कृपया इस मुद्दे को स्टार यह आपको प्रभावित करता है, तो: यहाँ एक कार्ड पृष्ठभूमि drawable कि काम करता है का एक उदाहरण है। https://code.google.com/p/android-developer-preview/issues/detail?id=731

उत्तर

8

CardView आरंभीकरण के दौरान अपनी ही ऊंचाई सेट है, जो स्थान पर आ जाएगी जो कुछ भी आप एक्सएमएल से निर्धारित किया है। आप https://code.google.com/p/android-developer-preview/wiki/FilingIssues?tm=3

@Override 
public void initialize(CardViewDelegate cardView, Context context, int backgroundColor, 
     float radius) { 
    cardView.setBackgroundDrawable(new RoundRectDrawable(backgroundColor, radius)); 
    View view = (View) cardView; 
    view.setClipToOutline(true); 
    view.setElevation(context.getResources().getDimension(R.dimen.cardview_elevation)); 
} 
+0

धन्यवाद के लिए कोड है, यह मैं क्या संदिग्ध पुष्टि करता है। मैं उस बग को फाइल करूंगा और उस समय में मैं एक अलग विधि का काम करूंगा। – IvonLiu

+0

@alanv क्या आप जानते हैं कि यह अभी भी एक बग है कि लॉलीपॉप आधिकारिक तौर पर बाहर है? मैं अभी भी इसे एक्सएमएल में काम नहीं कर सकता। – KickingLettuce

+1

आपको कार्ड एलिवेशन प्रॉपर्टी का उपयोग करना चाहिए (जैसा कि यज्ञ से दूसरे उत्तर में बताया गया है, जो कार्डव्यू के लिए डेवलपर है)। – alanv

0

में एक बग के रूप में इस फाइल तक bug ठीक जारी की है आप प्रोग्राम सेट कर सकते हैं चाहिए।

मैं एक RecyclerView इस तरह में यह किया है:

अपने RecyclerView.Adapter में:

public class MyAdapter extends RecyclerView.Adapter<TripsAdapter.ViewHolder> { 

    /* ... */ 

    public class ViewHolder extends RecyclerView.ViewHolder { 
     CardView card; 

     public ViewHolder(View itemView) { 
      card = (CardView) itemView.findViewById(R.id.card_view); 
     } 
    } 

    /* ... */ 

    @Override public void onBindViewHolder(ViewHolder holder, int position) { 
     /// ... 

     // Update Card Elevation 
     final float scale = mContext.getResources().getDisplayMetrics().density; 
     holder.card.setElevation(1f * scale); //TODO set this in xml once CardView elevation bug is fixed 
    } 

} 
+0

भी काम नहीं कर रहा है एलिवेशन विधि केवल एंड्रॉइड 5 में उपलब्ध है, इसलिए ओएस संस्करण जांच भी जोड़ने की आवश्यकता है। –

3

CardView पर ऊंचाई निर्धारित करने के लिए cardElevation संपत्ति का उपयोग करें। यह छाया आकार प्री-एल को भी प्रभावित करेगा।

इसके अलावा, अन्य विशेषताओं के लिए documentation देखें।

+0

ऐसा लगता है कि इसे आधिकारिक एसडीके जारी करने के बाद जोड़ा गया था। फिर भी, मुझे इस विशेषता के बारे में बताने के लिए धन्यवाद! प्री-लॉलीपॉप पर छाया आकार को आसानी से नियंत्रित करने की क्षमता एक निश्चित लाभ है। – IvonLiu

1

कृपया अपनी जड़

xmlns:card_view="http://schemas.android.com/apk/res-auto" 
नीचे

में इस ऐड एक cardview

<android.support.v7.widget.CardView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    card_view:cardElevation="10dp" 
    android:id="@+id/view" > 
    . 
    . 
    . 
    . 
    . 
    Child 
    . 
    . 
    . 
    . 

</android.support.v7.widget.CardView> 
संबंधित मुद्दे