【问题标题】:Google Now dropshadow on view显示 Google Now 阴影
【发布时间】:2013-06-05 15:50:49
【问题描述】:

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

是的,我的 Ninepatch 以 .9.png 结尾。

【问题讨论】:

  • 您是否使用 SDK 工具中的 draw9patch 工具创建了您的九个补丁?
  • @Karakuri 是的,是的,我做到了

标签: android nine-patch dropshadow google-now


【解决方案1】:

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

将此文件复制到card_background.9.png 并放在您的res/drawable-xhdpi 文件夹中,您也可以这样做:

res/drawable-hdpi 和:

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>

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

<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 的截图:

【讨论】:

  • 太棒了,我回家会试试这个。谢谢!
  • 你有 xxhdpi 的图片吗?
  • 我没有 xxhdpi 图像,但您应该可以从 google 音乐 APK 中获得一张。只需下载并解压缩即可。
  • 仅供参考,我刚刚下载了 Google 音乐 APK,他们不使用 XXHDPI 版本的card_bg_play.9.png
猜你喜欢
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-06
  • 2016-04-30
  • 2018-02-08
  • 1970-01-01
相关资源
最近更新 更多