AppCompat v21을 사용하여 안드로이드에서 floating action button(FAB)을 만드는 방법?
(목록 보기에 항목을 추가하는) 구글 캘린더와 같은 플로팅 액션 버튼을 만들어 롤리팝 이전 안드로이드 버전(5.0 이전)과의 호환성을 유지하고자 합니다.
다음 레이아웃을 만들었습니다.
활동 main_activity.xml:
<LinearLayout ... >
<include
layout="@layout/toolbar"/>
<RelativeLayout ... >
<!-- My rest of the layout -->
<!-- Floating action button -->
<ImageButton style="@style/AppTheme"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="New Button"
android:id="@+id/button"
android:src="@drawable/ic_fab"
android:background="@drawable/fab"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="24dp"
android:layout_marginRight="24dp"/>
</RelativeLayout>
</LinearLayout>
그리기 가능한 fab.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#ffa48bc0"/>
</shape>
styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#ff1d79b1</item>
<item name="colorPrimaryDark">#ff084d95</item>
</style>
</resources>
결과는 비슷하지만 재료 설계의 특징인 음영은 없습니다.
일정관리 이동 수행 단추:

내 앱의 이동 동작 버튼:

단추에 음영을 추가하려면 어떻게 해야 합니까?
속성 표고를 이미 사용했지만 작동하지 않습니다.
더 이상 자신만의 FAB를 만들거나 타사 라이브러리를 사용할 필요가 없는 AppCompat 22에 포함되어 있습니다.
https://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html
그라들 파일에 design in이라는 새로운 지원 라이브러리를 추가하기만 하면 됩니다.
compile 'com.android.support:design:22.2.0'
...당신은 가도 좋습니다.
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_happy_image" />
저는 일반적으로 xml 드로어블을 사용하여 미리 막대사탕 위젯에 그림자/경사를 만들어 왔습니다.예를 들어, 여기에는 사전 막대사탕 장치에서 부동 작업 버튼의 상승을 시뮬레이션하기 위해 사용할 수 있는 xml 그리기 기능이 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="8px">
<layer-list>
<item>
<shape android:shape="oval">
<solid android:color="#08000000"/>
<padding
android:bottom="3px"
android:left="3px"
android:right="3px"
android:top="3px"
/>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#09000000"/>
<padding
android:bottom="2px"
android:left="2px"
android:right="2px"
android:top="2px"
/>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#10000000"/>
<padding
android:bottom="2px"
android:left="2px"
android:right="2px"
android:top="2px"
/>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#11000000"/>
<padding
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px"
/>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#12000000"/>
<padding
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px"
/>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#13000000"/>
<padding
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px"
/>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#14000000"/>
<padding
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px"
/>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#15000000"/>
<padding
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px"
/>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#16000000"/>
<padding
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px"
/>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#17000000"/>
<padding
android:bottom="1px"
android:left="1px"
android:right="1px"
android:top="1px"
/>
</shape>
</item>
</layer-list>
</item>
<item>
<shape android:shape="oval">
<solid android:color="?attr/colorPrimary"/>
</shape>
</item>
</layer-list>
대신에?attr/colorPrimary어떤 색이든 선택하실 수 있습니다.다음은 결과의 스크린샷입니다.

앱에 FAB(Floating Action Button)를 추가하는 라이브러리가 많이 있습니다. 여기 제가 아는 것 중 몇 개가 있습니다.
이 모든 라이브러리는 pre-lolipop 장치에서 지원되며, 최소값은 api 8입니다.
여기 안드로이드용 무료 Floating Action Button 라이브러리가 있습니다. 많은 커스터마이징이 가능하고 SDK 버전 9 이상이 필요합니다.

@Justin Pollard xml 코드는 정말 잘 작동합니다.참고로 다음과 같은 xml 행으로 리플 효과를 추가할 수 있습니다.
<item>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight" >
<item android:id="@android:id/mask">
<shape android:shape="oval" >
<solid android:color="#FFBB00" />
</shape>
</item>
<item>
<shape android:shape="oval" >
<solid android:color="@color/ColorPrimary" />
</shape>
</item>
</ripple>
</item>
이 도서관을 이용해보세요. 그림자를 지원해주고 있습니다.minSdkVersion=7서포트도 해줍니다.android:elevation에 대한 속성API-21은연중에
원래 게시물은 여기 있습니다.
패딩 및 입면 추가:
android:elevation="10dp"
android:padding="10dp"
언급URL : https://stackoverflow.com/questions/26963219/how-to-create-a-floating-action-button-fab-in-android-using-appcompat-v21
'programing' 카테고리의 다른 글
| aria- expanded= "true"를 사용하여 CSS 속성을 변경하는 방법 (0) | 2023.09.21 |
|---|---|
| 사용자 프로세스 쉐도우 mysqld 서비스, 잘못된 명령은 어떻게 찾을 수 있습니까? (0) | 2023.09.21 |
| 열 1의 부분 집합 및 열 2의 범위에 대한 쿼리에 대한 MariaDB 인덱스 (0) | 2023.09.21 |
| 오라클에서 MINUS 연산자 (0) | 2023.09.16 |
| 일치조건시열값변경 (0) | 2023.09.16 |