【问题标题】:Shadow Separator Between Android FragmentsAndroid 片段之间的阴影分隔符
【发布时间】:2012-05-20 05:02:44
【问题描述】:

我的布局类似于平板电脑的 ICS Gmail 应用程序(左侧为ListFragment,右侧为内容),我想知道如何构建布局,以便在两者之间有一个阴影分隔符片段(如在 Gmail 应用中,如下所示)

.

另外,由于这适用于这个问题,我怎样才能在活动列表项的布局中拥有那个漂亮的三角形/箭头标记?我假设要实现这一点,ListView 本身必须位于 阴影“层”之上,但我不知道如何创建它。

【问题讨论】:

  • 这将是一个评论,因为它非常模糊。我不记得确切,但三角形与“单个项目选择”有关,您可以将可绘制对象应用于列表中选择的项目。 So really that triangle is some .png applied when an item on the list is selected.此外,阴影应该是“布局分隔线”,又是另一个 .png,应用于同一布局中的视图之间。

标签: android android-layout android-fragments android-listfragment


【解决方案1】:

只是为了让大家知道(因为似乎缺乏关于这个主题的信息),这是在单个列表行视图的背景选择器 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>

通过将这些定义为像这样的 9-patch 可绘制对象,您可以让每个列表项将其宽度的阴影贡献给中间的那条线,当它被激活时,该部分阴影将被箭头替换。我希望这对某人有所帮助,因为它肯定对我有所帮助!

【讨论】:

  • 例如,当列表只有三个项目时?如何在这一切上应用阴影?
【解决方案2】:

我希望做与您尝试做的事情相同的事情;创建一个片段比另一个片段“更接近”的效果。

Roboguy 的回答处理了如何在列表项上有白色箭头“选择器”;我将尝试更具体地说明阴影。 另一个使用背景选择器的好例子可以在 Google IO 2011 应用程序的源代码中看到。 获得源代码后,查看 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>

然后,将其实际应用到布局:

布局/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 包裹在相对布局中,然后添加渐变。

请注意,如果您这样做,渐变&lt;View&gt; 必须低于&lt;LinearLayout&gt;;否则,渐变会在线性布局下绘制,所以你看不到。

【讨论】:

  • 你在布局中使用了 fragment_shadow_left 但你创建了 shadow_left 资源。跨度>
猜你喜欢
  • 2017-02-01
  • 2016-09-24
  • 2015-02-15
  • 2011-06-10
  • 2021-08-14
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 2021-12-12
相关资源
最近更新 更多