Tôi có dự án Android Studio tối thiểu này chỉ bằng một nút. Tôi chỉ định cho nút một bóng với:Làm cách nào để mô phỏng độ cao nút (bóng) trong API Android thấp hơn 21?
android:elevation="3dp"
android:translationZ="3dp"
android:stateListAnimator="@null"
và tôi thấy bóng trong tab Thiết kế Android Studio. Nhưng tôi cũng nhận được một cảnh báo trong trình soạn thảo xml
Atribute ... chỉ được sử dụng ở mức API 21 và cao hơn (hiện tại tối thiểu là 16)
Và quả thực, nút bóng sẽ được hiển thị chỉ trong các trình giả lập với API level 21 or higher
và được hiển thị bằng phẳng, không có bóng ở tất cả các trình giả lập với API level lower than 21
.
Vì vậy, câu hỏi cụ thể là, làm thế nào tôi có thể mô phỏng hiệu ứng đổ bóng trong API's lower than 21
.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="@+id/main_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="2.24">
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginBottom="20dp"
android:elevation="5dp"
android:translationZ="5dp"
android:stateListAnimator="@null"
android:background="@android:color/holo_green_light"
android:text="BUTTON"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Cùng nút không hiển thị bóng, trong API 16
:
@Ramiro bạn có hiểu hay không ??? – Ironman
Vâng @Ironman, nó hoạt động. Nhưng nó đòi hỏi rất nhiều công việc đối phó với nút bấm, focusted, vv Vì vậy, tôi để lại xuất hiện nút như nó đã được dự định để được bởi Google trong mỗi API. Cảm ơn! – Ramiro