Google即时投影视图

Gui*_*ceU 4 android nine-patch dropshadow google-now

我想要像Google Now Cards这样令人敬畏的阴影.我该如何创建它?我一直在谷歌搜索,但没有找到我的问题的任何答案.我已经尝试了这个答案,但我只得到这样的结果:

是的,我的九点结束了.9.png.

小智 17

所以我做的工作就是使用card_background.9.pngGoogle Music 的九补丁文件:

card_background.9.png

将此文件复制到card_background.9.png您的res/drawable-xhdpi文件夹中,您也可以执行以下操作:

card_background.9.png

in res/drawable-hdpi和:

card_background.9.png

在res/drawable-mdpi.

接下来是内容视图和网格项布局.为了做到正确,你必须要小心你如何以及在哪里进行填充和所有重要的clipToPadding属性.首先是网格布局,这在活动中作为我的主要内容视图,在文件中board_grid_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/board_grid_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="0dp">    
    <GridView
            android:id="@+id/board_grid_view"
            style="@style/BoardGridView"
            />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是我在BoardGridView中使用的样式styles.xml,我对支持平板电脑/美化的常量进行了维度化,但为了便于使用,我在这里硬编码:

<style name="BoardGridView" parent="AppTheme">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:padding">8dp</item>
    <item name="android:clipToPadding">false</item>
    <item name="android:gravity">top|left</item>
    <item name="android:smoothScrollbar">true</item>
    <item name="android:cacheColorHint">#00000000</item>
    <item name="android:fastScrollEnabled">true</item>
    <item name="android:horizontalSpacing">8dp</item>
    <item name="android:verticalSpacing">8dp</item>
    <item name="android:numColumns">3</item>
    <item name="android:stretchMode">columnWidth</item>
    <item name="android:scrollbarStyle">outsideOverlay</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后是带有卡背景的网格项布局.这是我的布局文件board_grid_item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/grid_item"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:background="@drawable/card_background"
        >

        <RelativeLayout
                android:id="@+id/grid_item_thread_image_wrap"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                >

            <ImageView
                    android:id="@+id/grid_item_thread_thumb"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scaleType="centerCrop"
                    android:contentDescription="Thread thumbnail"
                    />
            <ImageView
                    android:id="@+id/grid_item_country_flag"
                    android:layout_width="18dp"
                    android:layout_height="11dp"
                    android:scaleType="fitXY"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentRight="true"
                    android:contentDescription="Thread country flag"
                    />
        </RelativeLayout>

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="84dp"
                android:orientation="vertical"
                >

            <TextView
                    android:id="@+id/grid_item_thread_subject"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="8dp"
                    android:paddingRight="8dp"
                    android:textColor="#444"
                    android:textSize="14sp"
                    android:maxLines="2"
                    />

            <TextView
                    android:id="@+id/grid_item_thread_info"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="8dp"
                    android:paddingRight="8dp"
                    android:textColor="#aaa"
                    android:textSize="12sp"
                    android:maxLines="3"
                    />

        </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

现在要将它们连接在一起,你需要一个游标加载器和适配器,但这不在问题的范围内.这种布局应该可以让您像Google音乐一样制作卡片视图.以下是在纵向模式下应用于Samsung Galaxy S3的上述九个补丁图像和xml的截图: