4

我想要这个很棒的阴影,比如Google Now Cards。我如何创建它?我一直在谷歌搜索,但没有找到任何问题的答案。我试过这个答案,但我只得到这个结果

是的,我的 Ninepatch 以.9.png.

4

1 回答 1

17

所以我为完成这项工作所做的就是使用card_background.9.png来自 Google Music 的九个补丁文件:

card_background.9.png

将此文件复制到card_background.9.png并放置在您的res/drawable-xhdpi文件夹中,您也可以对以下内容执行相同操作:

card_background.9.png

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>

这是我在 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>

然后是带有卡片背景的网格项目布局。这是我的布局文件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>

现在要将它连接在一起,您需要一个游标加载器和适配器,但这超出了问题的范围。这种布局应该可以让您制作像 Google Music 一样的卡片视图。以下是在我的三星 Galaxy S3 上以纵向模式应用的上述九个补丁图像和 xml 的截图:

于 2013-06-12T17:02:40.743 回答