在Android碎片之间的阴影分隔符
-
12-12-2019 - |
题
我有一个类似于ICS Gmail应用程序的平板电脑(左侧的生成Gmail应用程序和右侧的内容),我想知道我如何能够建设布局,使得两个片段之间存在暗影分隔符(喜欢在Gmail应用程序中,如下所示)
。
也是适用于这个问题的,我如何在活动列表项的布局中有那个漂亮的三角/箭头标记?我假设要实现这一点,ListView本身必须撒谎上方阴影“图层”,但我不知道如何创建它。
解决方案
只是为了让每个人都知道(因为似乎在这个主题上缺乏信息),这是在各个列表行视图的背景选择器XML中实现的。例如,这是主屏幕的布局,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/list_item_selector">
...<!-- Rest of layout goes here -->
</RelativeLayout>
.
但是魔术来自list_item_selector
:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/list_pressed" />
<item android:state_activated="true" android:drawable="@drawable/list_arrow_activated" />
<item android:drawable="@drawable/list_default" />
</selector>
.
通过定义这些像素如此,您可以让每个列表项贡献它在中间的那条线上的宽度的阴影,并且当它激活时,阴影段将被箭头替换。我希望这有助于某人,因为它确实帮助了我!
其他提示
我想做你想要做的同样的事情;创建一个片段是“更近”的效果。
Roboguy的答案处理如何在列表项上具有白色箭头“选择器”;我会尝试更具体地了解阴影。 在Google IO 2011 App的源代码中可以看到使用背景选择器的另一个良好示例。有源,查看fragment_dashboard.xml图标选择器。
暗影分离器是梯度,施加到视图的左侧。有两部分;
首先,“阴影”本身:
res / shadow_left.xml
. 然后,要实际将其应用于布局:<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:angle="0" android:startColor="#55000000" android:endColor="#00000000" /> </shape>
layout / my_lower_layer
.<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <View android:layout_width="20dp" android:layout_height="fill_parent" android:background="@drawable/fragment_shadow_left" /> <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout>
必须用相对布局(对我的知识)进行。如果您使用的是线性布局,则可以将整个LinearLayout包装在相对布局内部,然后添加渐变。
请注意,如果这样做,渐变生成的
<View>
必须在<LinearLayout>
以下;否则,将在线性布局绘制梯度,因此您不会看到它。