[android] Android에서 선형 레이아웃 주위에 그림자를 표시하는 방법은 무엇입니까?

선형 레이아웃에 그림자를 표시하려면 어떻게해야합니까? 선형 레이아웃 주위에 그림자가있는 흰색 둥근 배경을 원합니다. 나는 지금까지 이것을했다.

<LinearLayout
 android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@xml/rounded_rect_shape"
android:orientation="vertical"
android:padding="10dp">
<-- My buttons, textviews, Imageviews go here -->
</LinearLayout>

그리고 xml 디렉토리 아래의 rounded_rect_shape.xml

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

   <solid android:color="#ffffff" />

   <corners
      android:bottomLeftRadius="3dp"
      android:bottomRightRadius="3dp"
      android:topLeftRadius="3dp"
      android:topRightRadius="3dp" />
</shape>



답변

Android에는 그림자를 표시하는 이러한 속성이 없습니다. 그러나 가능한 방법은 다음과 같습니다.

  1. 회색 색상의 일반 LinearLayout을 추가하고, 그 위에 실제 레이아웃을 추가하고 아래쪽에 여백이 있고 오른쪽은 1 또는 2dp와 같습니다.

  2. 그림자가있는 9 패치 이미지를 가지고 선형 레이아웃의 배경으로 설정하십시오.


답변

LinearLayoout의 배경 역할을 할 레이어 목록을 구현하여 문제에 대한 또 다른 해결책이 있습니다.

background_with_shadow.xml 파일을 res/drawable. 포함 :

<?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="@android:color/darker_gray" />
        <corners android:radius="5dp"/>
        </shape>
    </item>
    <item android:right="1dp" android:left="1dp" android:bottom="2dp">
        <shape
            android:shape="rectangle">
        <solid android:color="@android:color/white"/>
        <corners android:radius="5dp"/>
        </shape>
    </item>
</layer-list>

그런 다음 LinearLayout에서 레이어 목록을 배경으로 추가하십시오.

<LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/background_with_shadow"/>


답변

글쎄, 이것은 달성하기 쉽습니다.

GradientDrawable그림자를 가지려는 뷰에 모양을 가깝게 배치하기 위해 부모 관계를 사용하는 것보다 검정색에서 나오고 투명한 색상으로가는 a 를 빌드 한 다음 높이 또는 너비에 값을 지정하면됩니다.

다음은이 파일을 내부 res/drawable에 생성해야하는 예입니다. 이름은 다음과 shadow.xml같습니다.

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

    <gradient
        android:startColor="#9444"
        android:endColor="#0000"
        android:type="linear"
        android:angle="90"> <!-- Change this value to have the correct shadow angle, must be multiple from 45 -->
    </gradient>

</shape>

A로부터 위의 다음 코드를 넣습니다 LinearLayout예를 들어, 설정 android:layout_widthandroid:layout_heightfill_parent하고 2.3dp, 당신은 당신에 좋은 그림자 효과를해야합니다 LinearLayout.

<View
    android:id="@+id/shadow"
    android:layout_width="fill_parent"
    android:layout_height="2.3dp"
    android:layout_above="@+id/id_from_your_LinearLayout"
    android:background="@drawable/shadow">
</View>

1 주 : 당신이 증가하면 android:layout_height더 그림자가 표시됩니다.

참고 2 :android:layout_above="@+id/id_from_your_LinearLayout" 이 코드를 RelativeLayout 내에 배치하는 경우 속성을 사용 하고 그렇지 않으면 무시하십시오.

누군가를 돕기를 바랍니다.


답변

롤리팝 이상에서는 고도 를 사용할 수 있습니다 .

이전 버전의 경우 :

다음은 게으른 해킹입니다.
http://odedhb.blogspot.com/2013/05/android-layout-shadow-without-9-patch.html

(toast_frame은 KitKat에서 작동하지 않으며 그림자는 토스트에서 제거되었습니다)

다음을 사용하십시오.

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

또는:

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

배경으로

예 :

<TextView
        android:layout_width="fill_parent"
        android:text="I am a simple textview with a shadow"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:padding="16dp"
        android:textColor="#fff"
        android:background="@android:drawable/toast_frame"
        />

그리고 다른 bg 색상으로 :

<LinearLayout
        android:layout_height="64dp"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:background="@android:drawable/toast_frame"
        android:padding="4dp"
        >
    <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Button shadow"
            android:background="#33b5e5"
            android:textSize="24sp"
            android:textStyle="bold"
            android:textColor="#fff"
            android:layout_gravity="center|bottom"
            />

</LinearLayout>


답변

이것을 시도하십시오 .. layout_shadow.xml

<?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="#CABBBBBB"/>
            <corners android:radius="2dp" />
        </shape>
    </item>

    <item
        android:left="0dp"
        android:right="0dp"
        android:top="0dp"
        android:bottom="2dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <corners android:radius="2dp" />
        </shape>
    </item>
</layer-list>

다음과 같이 레이아웃에 적용하십시오.

 android:background="@drawable/layout_shadow"


답변

나는 이것이 오래되었다는 것을 알고 있지만 대부분의 답변에는 많은 추가 코드가 필요합니다.

밝은 색상의 배경이있는 경우 다음을 사용하면됩니다.

android:elevation="25dp"


답변

실제로 @odedbreiner에 동의하지만 첫 번째 레이어 안에 dialog_frame을 넣고 흰색 레이어 아래에 검은 색 배경을 숨 깁니다.

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