program tip

Android 5.0의 AppCompat CardView에서 XML의 고도 설정

radiobox 2020. 8. 31. 07:38
반응형

Android 5.0의 AppCompat CardView에서 XML의 고도 설정


내가 이해하는 바에 따르면, 미리보기 단계의 초기에는 CardViewJava에서 해킹하지 않고 s 에서만 XML로 고도를 설정할 수있는 방법이 없었습니다 . 이제 공식 릴리스가 나왔으니 Java 코드를 작성하지 않고 XML로이를 수행 할 수있는 방법이 있습니까?

나는 card_view:cardElevation효과가 없도록 노력 했다. 5.0 에뮬레이터를 사용할 때 모든 것이 괜찮다고 생각했습니다. 하지만 이제 실제 기기에서 공식 버전을 사용하고 있으므로 모든 것이 CardView사라졌습니다.

Pre Lollipop, 훌륭하게 작동합니다.

여기 내 전체 XML이 있습니다.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:id="@+id/cv1"
    card_view:cardElevation="4dp"
    android:layout_margin="6dp"
    card_view:cardCornerRadius="3dp"
    android:layout_height="match_parent">

여백 / 패딩 문제처럼 보입니다 . cardUseCompatPadding 속성을 true 로 설정해보십시오 . 예 :

<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="6dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardElevation="4dp"
    card_view:cardCornerRadius="3dp">

Android 문서의 설명 :

CardView는 L 이전의 플랫폼에서 그림자를 그리기 위해 추가 패딩을 추가합니다.

이로 인해 카드의 크기가 L과 L 이전 사이에 다를 수 있습니다. CardView를 다른 뷰와 정렬해야하는 경우 변경 사항을 설명하기 위해 API 버전 별 차원 리소스가 필요할 수 있습니다. 대안으로 cardUseCompatPadding 플래그를 true로 설정할 수 있으며 CardView는 플랫폼 L 및 이후에 동일한 패딩 값을 추가합니다.

cardUseCompatPadding 플래그를 true로 설정하면 UI에 불필요한 간격이 추가되므로 기본값은 false입니다.


이 줄이 있으면

android:hardwareAccelerated="false"

매니페스트 응용 프로그램 태그에서 그림자가 표시되지 않았습니다. 이 줄을 제거하십시오

또는 사용

android:hardwareAccelerated="true"

이것은 나를 위해 일했습니다! 나는 그것이 당신에게도 효과가 있기를 바랍니다 :)


cardElevation 속성 을 사용해야 합니다.

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:cardElevation="4dp" />

그것은 추가하여 나를 해결했습니다.

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

예를 들면 :

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    card_view:cardCornerRadius="5dp">

i am adding an app:cardElevation="8dp" attribute on card view, so it will be like :

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        app:cardElevation="8dp"
        app:cardCornerRadius="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:src="@drawable/kmp_warna1" />

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

Hope it will help

참고URL : https://stackoverflow.com/questions/26997109/setting-elevation-in-xml-on-appcompat-cardview-on-android-5-0

반응형