5

私はこのボタンを使って、この最小のAndroid Studioプロジェクトを持っています。 私はボタンを影を割り当てる:Android APIのボタンの高さ(シャドー)を21未満にエミュレートするにはどうすればよいですか?

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

と私は、Android Studioの[デザイン]タブに影を参照してください。しかし、私はまた、唯一のAPIレベル21以降で使用されているXMLエディタ

Atribute ...で警告を受ける

(現在の分は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で影を示すボタン、:で enter image description here

影を示していない同じボタン、: enter image description here

答えて

5

Pre-LolliPopのデバイスでは、ドロウアブルファイルを使用してシャドーまたは標高を作成する必要があります。こちらです。

<?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> 

が次にあなたが欲しいする要素に次のコードを追加しますelement_background.xmlようなあなたのフォルダにdrawableファイルを作成します。

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

@Ramiroあなたはそれを取得したりしませんでした? – Ironman

+1

はい@アイロンマン、それは動作します。しかし、それはボタンの押された、focustedなどを扱う多くの仕事を必要とするので、私はボタンの外観を各APIのGoogleが意図していたままにしておきます。ありがとう! – Ramiro

5

希望する場合はCarbonを試すことができます。 2.2に戻るバックボーンの資料機能です。標高と生成されたアニメーションシャドウもサポートしています。

https://github.com/ZieIony/Carbon

関連する問題