activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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" android:padding="16dp" tools:context=".MainActivity" android:background="#ffffff" > <!-- CardView A FrameLayout with a rounded corner background and shadow. CardView uses elevation property on L for shadows and falls back to a custom shadow implementation on older platforms. --> <!-- android.support.v7.cardview:cardElevation Elevation for CardView. Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters). This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type. This is a private symbol. --> <!-- android.support.v7.cardview:cardMaxElevation Maximum Elevation for CardView. Must be a dimension value. --> <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view_left" android:layout_width="wrap_content" android:layout_height="wrap_content" card_view:cardCornerRadius="5dp" card_view:contentPadding="25dp" card_view:cardBackgroundColor="#d5efd4" card_view:cardElevation="6dp" card_view:cardMaxElevation="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SAMPLE\nCARDVIEW" android:textSize="30sp" android:textStyle="bold" android:layout_gravity="center" android:gravity="center" android:textColor="#2ca92a" /> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view_right" android:layout_width="wrap_content" android:layout_height="wrap_content" card_view:cardCornerRadius="5dp" card_view:contentPadding="25dp" card_view:cardBackgroundColor="#d5efd4" android:layout_toRightOf="@id/card_view_left" android:layout_toEndOf="@id/card_view_left" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ANOTHER\nCARDVIEW" android:textSize="30sp" android:textStyle="bold" android:layout_gravity="center" android:gravity="center" android:textColor="#2ca92a" /> </android.support.v7.widget.CardView> </RelativeLayout>
MainActivity.java
package com.cfsuman.me.androidcode; import android.content.Context; import android.content.res.Resources; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.CardView; import android.util.TypedValue; import android.view.Window; public class MainActivity extends AppCompatActivity { private Context mContext; private CardView mCardViewLeft; private CardView mCardViewRight; @Override protected void onCreate(Bundle savedInstanceState) { // Request window feature action kafe requestWindowFeature(Window.FEATURE_ACTION_BAR); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the application context mContext = getApplicationContext(); // Change the action kafe color getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFF0004"))); // Get the widgets reference from XML layout mCardViewLeft = (CardView) findViewById(R.id.card_view_left); mCardViewRight = (CardView) findViewById(R.id.card_view_right); /* public void setMaxCardElevation (float radius) Updates the backward compatible elevation of the CardView. Calling this method has no effect if device OS version is L or newer and getUseCompatPadding() is false. Related XML Attributes android.support.v7.cardview:cardElevation Parameters radius : The backward compatible elevation in pixels. */ // Set the card maximum elevation // IMPORTANT : first set the card maximum elevation then elevation. mCardViewRight.setMaxCardElevation(getPixelsFromDPs(10)); /* public void setCardElevation (float radius) Updates the backward compatible elevation of the CardView. Related XML Attributes android.support.v7.cardview:cardElevation Parameters radius : The backward compatible elevation in pixels. */ // Set the card elevation mCardViewRight.setCardElevation(getPixelsFromDPs(6)); } // Custom method for converting DP/DIP value to pixels protected int getPixelsFromDPs(int dps){ Resources r = mContext.getResources(); int px = (int) (TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, dps, r.getDisplayMetrics())); return px; } }
build.gradle [dependencies]
compile 'com.android.support:cardview-v7:23.0.1'
Komentar
Posting Komentar