5

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); 
    } 

} 

Nút thấy bóng, trong API 22: enter image description here

Cùng nút không hiển thị bóng, trong API 16: enter image description here

Trả lời

5

Thiết bị bạn phải tạo bóng hoặc độ cao bằng tệp có thể kéo. Cách này.

Tạo drawable tập tin trong thư mục của bạn như element_background.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="#BDBDBD"/> 
     <corners android:radius="5dp"/> 
    </shape> 
</item> 

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

Sau đó, thêm đoạn mã sau vào các yếu tố mà bạn muốn.

android:background="@drawable/element_background" 
+0

@Ramiro bạn có hiểu hay không ??? – Ironman

+1

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

5

Nếu bạn muốn, bạn có thể thử Carbon. Đó là một thư viện backporting các tính năng vật liệu trở lại 2.2. Nó hỗ trợ độ cao và tạo ra, bóng hoạt hình là tốt.

https://github.com/ZieIony/Carbon

Các vấn đề liên quan