Android 新的 Google 即时和 Google+ 卡片界面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11639991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
New Google Now and Google+ card interface
提问by HBG
Google Now and Google+ (Android) both make use of a card-like interface.
Google Now 和 Google+ (Android) 都使用类似卡片的界面。
I was wondering if anyone had any idea how this interface can be replicated on Android.
我想知道是否有人知道如何在 Android 上复制此界面。
They both have quite interesting animations for displaying new cards too; any thoughts would be great.
它们都有非常有趣的动画来展示新卡片;任何想法都会很棒。
采纳答案by Shardul
I have posted a tutorial on how to replicate / create Google Cards style layout here.
我在此处发布了有关如何复制/创建 Google Cards 样式布局的教程。
Key steps
关键步骤
- Create a custom layout
- Add observer for drawing children
- Animate alternating cards
- 创建自定义布局
- 添加观察者以绘制儿童
- 动画交替卡片
Heres a code snippet
这是一个代码片段
@Override
public void onGlobalLayout() {
getViewTreeObserver().removeGlobalOnLayoutListener(this);
final int heightPx = getContext().getResources().getDisplayMetrics().heightPixels;
boolean inversed = false;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
int[] location = new int[2];
child.getLocationOnScreen(location);
if (location[1] > heightPx) {
break;
}
if (!inversed) {
child.startAnimation(AnimationUtils.loadAnimation(getContext(),
R.anim.slide_up_left));
} else {
child.startAnimation(AnimationUtils.loadAnimation(getContext(),
R.anim.slide_up_right));
}
inversed = !inversed;
}
}
回答by userM1433372
Take a look at http://ryanharter.com/blog/2013/01/31/how-to-make-an-android-card-list/
看看http://ryanharter.com/blog/2013/01/31/how-to-make-an-android-card-list/
Copy of the example:
示例副本:
/res/drawable/bg_card.xml:
/res/drawable/bg_card.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle"
android:dither="true">
<corners android:radius="2dp"/>
<solid android:color="#ccc" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle"
android:dither="true">
<corners android:radius="2dp" />
<solid android:color="@android:color/white" />
<padding android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
</shape>
</item>
</layer-list>
Use it as the background of your layout:
将其用作布局的背景:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="12dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:background="@drawable/bg_card">
<!-- Card Contents go here -->
</LinearLayout>
</FrameLayout>
回答by userM1433372
==== Start update 2014-09-29 ====
==== 开始更新 2014-09-29 ====
Use the CardView from the Google Compatibility library (from Android 2.1+):
使用 Google 兼容性库中的 CardView(来自 Android 2.1+):
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp">
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.CardView>
See https://developer.android.com/preview/material/ui-widgets.html
请参阅https://developer.android.com/preview/material/ui-widgets.html
==== End update ====
==== 结束更新 ====
(at least) two options:
(至少)两个选项:
or
或者
- https://github.com/afollestad/Cards-UIis a UI library for showing Cards.
- https://github.com/afollestad/Cards-UI是一个用于显示卡片的 UI 库。
See https://github.com/afollestad/Cards-UI/wiki/2.-Intro-Tutorialfor an simple introduction.
有关简单介绍,请参阅https://github.com/afollestad/Cards-UI/wiki/2.-Intro-Tutorial。
回答by confucius
I made a very similar Layout you can look at here https://github.com/Nammari/GooglePlusLayoutand for a video demo here http://youtu.be/jvfDuJz4fw4for the animation that get applied to children , for more details look here http://nammari.tumblr.com/post/41893669349/goolge-plus-layoutfor a blog post that clarify every thing .
我制作了一个非常相似的布局,您可以在此处查看https://github.com/Nammari/GooglePlusLayout并在此处 查看视频演示http://youtu.be/jvfDuJz4fw4以了解应用于儿童的动画,有关更多详细信息,请查看这里是 http://nammari.tumblr.com/post/41893669349/goolge-plus-layout的博客文章,阐明了每一件事。
回答by Maria Neumayer
The card look and feel shouldn't be hard. You just need a ListView without dividers and your list view items should have a margin.
卡片的外观和感觉应该不难。您只需要一个没有分隔符的 ListView 并且您的列表视图项应该有一个边距。
Like this:
像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_margin="16dp"
android:layout_height="wrap_content"
android:background="@android:color/background_light">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingLeft="16dp"
android:text="Title"
android:textSize="18dp"
android:textColor="@android:color/primary_text_holo_light"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="16dp"
android:paddingLeft="16dp"
android:text="Subtitle"
android:textSize="14dp"
android:textColor="@android:color/primary_text_holo_light"
/>
<ImageView android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"/>
</LinearLayout>
回答by dparnas
Have the same need, and have started investigating a bit. I've been looking at the apktool output I've been able to get from the com.google.android.googlequicksearchbox apk. (no sources only the res xmls)
有同样的需求,已经开始研究了一下。我一直在查看能够从 com.google.android.googlequicksearchbox apk 获得的 apktool 输出。(没有来源只有 res xmls)
This layout (at_place_card.xml) is used for displaying a location. It has three text lines and two actions buttons (details and checkin) on the right and an image on the left.
此布局 (at_place_card.xml) 用于显示位置。它的右侧有三个文本行和两个操作按钮(详细信息和签到),左侧有一个图像。
Unfortunately, I am not able to get any style information out from the apk, so the font size, dimensions and colors are just guesses.
不幸的是,我无法从 apk 中获取任何样式信息,因此字体大小、尺寸和颜色只是猜测。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="horizontal" android:background="@drawable/card_background" android:layout_width="fill_parent" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:thegoogle="http://schemas.android.com/apk/res/com.google.android.googlequicksearchbox">
<LinearLayout android:orientation="vertical" android:layout_width="0.0dip" android:layout_height="fill_parent" android:baselineAligned="false" android:layout_weight="1.0">
<FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" style="@style/CardTextBlock">
<TextView android:id="@id/entry_title" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/CardTitle" />
<TextView android:id="@id/open_hours" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/CardText" />
<TextView android:textColor="@color/card_light_text" android:id="@id/known_for_terms" android:paddingBottom="4.0dip" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="4" style="@style/CardText" />
</LinearLayout>
<ImageButton android:layout_gravity="top|right|center" android:id="@id/card_menu_button" android:layout_width="@dimen/card_action_button_height" android:layout_height="@dimen/card_action_button_height" android:contentDescription="@string/accessibility_menu_button" style="@style/CardMenuButton" />
</FrameLayout>
<Space android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" />
<Button android:id="@id/details_button" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="@dimen/card_action_button_height" android:text="@string/more_details" android:drawableLeft="@drawable/ic_action_pin" style="@style/CardActionButtonWithIcon" />
<Button android:id="@id/checkin_button" android:layout_width="fill_parent" android:layout_height="@dimen/card_action_button_height" android:text="@string/check_in" android:drawableLeft="@drawable/ic_action_check_in" style="@style/CardActionButtonWithIcon" />
</LinearLayout>
<com.google.android.velvet.ui.CrossfadingWebImageView android:id="@id/place_photo" android:visibility="gone" android:layout_width="0.0dip" android:layout_height="fill_parent" android:scaleType="centerCrop" android:adjustViewBounds="true" android:baselineAligned="false" android:minHeight="@dimen/at_place_card_content_height" android:layout_weight="1.0" android:contentDescription="@string/at_place_card_image_desc" thegoogle:crossfadeDuration="@integer/image_crossfade_duration" />
</LinearLayout>
Update: Was able to get some style information also now. If you're interested here is a zip file with the information I have currently (some resource files from google now). https://dl.dropbox.com/u/4379928/android/googlenow2.zip
更新:现在也可以获得一些样式信息。如果您有兴趣,这里有一个 zip 文件,其中包含我目前拥有的信息(现在来自 google 的一些资源文件)。 https://dl.dropbox.com/u/4379928/android/googlenow2.zip