10

ボタンだけのこの最小限の Android Studio プロジェクトがあります。ボタンに影を割り当てます:

android:elevation="3dp"
android:translationZ="3dp"
android:stateListAnimator="@null"

Android Studioの[デザイン]タブに影が表示されます。しかし、xmlエディターでも警告が表示されます

Atribute ... は API レベル 21 以降でのみ使用されます (現在の最小値は 16 です)

実際、ボタンの影は のエミュレータでのみ表示され、 のエミュレータでAPI level 21 or higherは影がまったくなく、フラットに表示されますAPI level lower than 21

具体的な問題は、 で影の効果をどのようにエミュレートできるかということです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);
    }

}

影を示すボタンAPI 22: ここに画像の説明を入力

影を表示しない同じボタンAPI 16: ここに画像の説明を入力

4

2 に答える 2

6

If you wish, you can try Carbon. It's a library backporting material features back to 2.2. It supports elevation and generated, animated shadows as well.

https://github.com/ZieIony/Carbon

于 2016-07-10T22:59:22.970 に答える