Android: 在 ViewCard 上显示视图
Android: Displaying a view over ViewCard
我想实现这个(CardView 上的功能区):
这是我的解决方案的片段:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ribbonParentId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:layout_marginBottom="15dp"
android:background="@drawable/green_ribbon" >
<android.support.v7.widget.AppCompatTextView
android:id="@+id/ribbonId"
style="@style/RibbonStyle"
android:text="@string/ribbon_text" />
</LinearLayout>
<android.support.v7.widget.CardView
android:id="@+id/historicalWeightCardViewId"
style="@style/MyCardViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp" >
... Other elements
</android.support.v7.widget.CardView>
在 RecycleView 适配器中,我通过调用将功能区置于最前面。
itemView.findViewById(R.id.ribbonParent2Id).bringToFront();
我的解决方案适用于我测试过的大多数设备,但 Nexus 5 (Android 5.1.1) 除外,其中功能区仍(部分)位于 cardview 后面。我正在使用 AppCompat 并定位 >v4.0。
- 将视图放在卡片视图上的正确方法是什么?还有另一种方法可以实现同样的目标吗?为什么我的解决方案在某些情况下不起作用?
- 如何让绿丝带从屏幕的左边缘开始?目前在熨平板边缘和色带起点之间仍然存在 space。
这是由 elevation
引起的,因为 android 21 上的 CardView 使用 true elevation
并且您的 LinearLayout 的高度为 0,因此它在 CardView 后面。
要解决这个问题,您有 2 个选择
- 将 android:elevation 设置为您的
LinearLayout
,例如android:elevation="10px"
用下面的 FrameLayout
包装你的 CardView
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/historicalWeightCardViewId"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="18dp" >
</android.support.v7.widget.CardView>
</FrameLayout>
<LinearLayout
android:id="@+id/ribbonParentId"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="left|top"
android:layout_marginBottom="15dp"
android:background="@android:color/holo_red_dark">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ribbonId"
android:text="abc" />
</LinearLayout>
我想实现这个(CardView 上的功能区):
这是我的解决方案的片段:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ribbonParentId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:layout_marginBottom="15dp"
android:background="@drawable/green_ribbon" >
<android.support.v7.widget.AppCompatTextView
android:id="@+id/ribbonId"
style="@style/RibbonStyle"
android:text="@string/ribbon_text" />
</LinearLayout>
<android.support.v7.widget.CardView
android:id="@+id/historicalWeightCardViewId"
style="@style/MyCardViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp" >
... Other elements
</android.support.v7.widget.CardView>
在 RecycleView 适配器中,我通过调用将功能区置于最前面。
itemView.findViewById(R.id.ribbonParent2Id).bringToFront();
我的解决方案适用于我测试过的大多数设备,但 Nexus 5 (Android 5.1.1) 除外,其中功能区仍(部分)位于 cardview 后面。我正在使用 AppCompat 并定位 >v4.0。
- 将视图放在卡片视图上的正确方法是什么?还有另一种方法可以实现同样的目标吗?为什么我的解决方案在某些情况下不起作用?
- 如何让绿丝带从屏幕的左边缘开始?目前在熨平板边缘和色带起点之间仍然存在 space。
这是由 elevation
引起的,因为 android 21 上的 CardView 使用 true elevation
并且您的 LinearLayout 的高度为 0,因此它在 CardView 后面。
要解决这个问题,您有 2 个选择
- 将 android:elevation 设置为您的
LinearLayout
,例如android:elevation="10px"
用下面的
FrameLayout
包装你的CardView
<FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.CardView android:id="@+id/historicalWeightCardViewId" android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="18dp" > </android.support.v7.widget.CardView> </FrameLayout> <LinearLayout android:id="@+id/ribbonParentId" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_gravity="left|top" android:layout_marginBottom="15dp" android:background="@android:color/holo_red_dark"> <android.support.v7.widget.AppCompatTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ribbonId" android:text="abc" /> </LinearLayout>