http://imgur.com/KafNkf8[Android L] Android Material nút tròn
Vì vậy, làm thế nào ai đó sẽ đi về thực hiện vào nút hình tròn được trưng bày trong android L? Ngoài ra tên kỹ thuật của nút tròn, mã XML sẽ được đánh giá cao là gì.
http://imgur.com/KafNkf8[Android L] Android Material nút tròn
Vì vậy, làm thế nào ai đó sẽ đi về thực hiện vào nút hình tròn được trưng bày trong android L? Ngoài ra tên kỹ thuật của nút tròn, mã XML sẽ được đánh giá cao là gì.
Các nút được gọi là FAB (Floating Action Button), nó khá đơn giản để thực hiện .
Activity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutfab);
//Outline
int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
Outline outline = new Outline();
outline.setOval(0, 0, size, size);
findViewById(R.id.fab).setOutline(outline);
}
}
anim.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="@dimen/button_elevation"
android:valueTo="@dimen/button_press_elevation"
android:valueType="floatType" />
</item>
<item>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="@dimen/button_press_elevation"
android:valueTo="@dimen/button_elevation"
android:valueType="floatType" />
</item>
</selector>
dimens.xml
<resources>
<dimen name="fab_size">56dp</dimen>
<dimen name="button_elevation">2dp</dimen>
<dimen name="button_press_elevation">4dp</dimen>
</resources>
và file layout của bạn để xác định nút FAB.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/fab"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="@dimen/fab_size"
android:layout_height="@dimen/fab_size"
android:layout_gravity="bottom|right"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/ripple"
android:stateListAnimator="@anim/anim"
android:src="@drawable/ic_action_add"
android:elevation="4dp"
/>
</RelativeLayout>
Cuối cùng, gợn ảnh hưởng khi bạn nhấn nút. ripple.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
<item>
<shape android:shape="oval">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
Thưởng thức FAB mới của bạn. Hãy ghi nhớ điều này là dành cho android L chỉ!
EDIT này đã được cập nhật tại địa chỉ: https://stackoverflow.com/a/24548910/4352176
Hy vọng rằng sẽ có thư viện tương thích được Google phát hành khi Android L sẽ xuất hiện ở đó! – chteuchteu
Có ai thử với bản vẽ thay thế cho ripple.xml, cho các phiên bản L trước không? Tôi có một vấn đề khi tôi xác định drawable và drawable-v21 thư mục .. Trong drawable tôi sử dụng hình dạng và drawable-21 tôi sử dụng gợn, nhưng ripple.xml của tôi cho L không bao giờ xuất hiện trên L phiên bản ... – Sandra
Xin chào Sandra, SDK xem trước do Google phát hành không có nghĩa là để sản xuất, nếu bạn quản lý để bật các chủ đề/tính năng Android L trong các phiên bản trước của Android, nó sẽ là phương pháp rất nhanh và dơ bẩn. Tất nhiên là tùy thuộc vào bạn nếu bạn muốn triển khai các API chưa hoàn thành. Khi tôi bắt đầu phát triển ứng dụng của mình, tôi đã hết sức sử dụng các API mới, được suy nghĩ qua trải nghiệm của mình với ứng dụng, tôi đã phải hoàn nguyên về API thấp hơn vì Android L chưa sẵn sàng. Tôi khuyên bạn nên làm theo hướng dẫn thiết kế của Google [link] (http://www.google.com/design/spec/material-design/introduction.html) – M0NKEYRASH
Bạn cần xác định một đối tượng khác có thể vẽ được. Nó khá đơn giản, nhưng không có giấy tờ.
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
<item>
<shape android:shape="oval">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
tôi nhận này từ đây: https://github.com/romainguy/google-io-2014/blob/7c174c7901d8cd2601807dcd17f3df7ed88fbee9/app/src/main/res/drawable/info_background.xml
Đừng quên để thiết lập độ cao và đề cương
Có một dễ dàng hơn và cho tất cả các phiên bản (v7) cách:
<ImageButton style="@style/AppTheme"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="New Button"
android:id="@+id/OkBt"
android:elevation="10dp"
android:src="@drawable/ic_keyboard_return_black_36dp"
android:background="@drawable/circle" />
Nhìn vào style = "@ style/AppTheme" Kiểu mà tôi đã đặt cho phiên bản cũ là:
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
Đối với kẹo Android là:
<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
Các drawable hình cho vòng tròn là:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#81D4FA"/>
</shape>
Nó hoạt động tốt cho tất cả các phiên bản android, nó làm tự động bóng tròn trong Lollipop.
Trả lời tại đây: http://stackoverflow.com/questions/9884202/custom-circle-button – ucdevs