【问题标题】:White border around dark themed CardViews深色主题 CardViews 周围的白色边框
【发布时间】:2016-03-04 16:12:41
【问题描述】:

我有一个扩展 CardView 的自定义控件。我将它添加到线性布局中,以便创建卡片网格。我没有使用 ListView 或 RecyclerView。

我想在线性布局中的卡片之间设置一个间隙,所以我定义了一个边距。

卡片布局使用深色主题。我的应用程序使用默认材质主题(深色)。我正在 Pixel C、Android 6.0.1 上进行测试。

<android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        card_view:cardUseCompatPadding="true"
        card_view:cardPreventCornerOverlap="false"
        card_view:cardElevation="5dp"
        style="@style/CardView.Dark">
<!-- content here -->
</android.support.v7.widget.CardView>

我将它们添加到列表视图中:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
        TableRow.LayoutParams.WRAP_CONTENT);
LinearLayout newLinearLayout = new LinearLayout(this);
newLinearLayout.setLayoutParams(layoutParams);
newLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

mainLayoutContainer.addView(newLinearLayout);
newLinearLayout.addView(myCardLayoutObj);

将布局与 CardView 一起使用的类实际上扩展了 CardView 本身,例如

public class MyCustomWidgetextends CardView{
    public MyCustomWidget(Context context) {
        super(context);

        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.my_card_view_layout, this);
}

我看到的是这个。卡片内容部分看起来不错..深色。但是 CardView 周围的边距/间隙是白色的。我如何让它变得透明,为什么暗卡主题会这样?另外,在这样的深色主题上,阴影会是什么样子?

【问题讨论】:

  • 您是否尝试过将newLinearLayout 的背景属性设置为您想要的颜色?
  • 这样做似乎没有任何效果。只有卡片布局周围的边距是白色的,而不是线性布局本身。

标签: android layout android-cardview


【解决方案1】:

所以我有一个包含&lt;CardView&gt; 的布局文件,如上所述。我的自定义类扩展了这个布局扩展了 CardView。所以,我在 CardView 中有一个 CardView。 “外部”卡片视图,即我的自定义类扩展的视图,从未设置过主题,因此它使用默认的“浅色”主题。

因为我是以编程方式创建这个小部件,所以我认为我需要扩展 CardView。这是错误的。

之前:

public class MyCustomWidget extends CardView

之后:

public class MyCustomWidget extends LinearLayout{
    public MyCustomWidget(Context context) {
        super(context);

        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.my_card_view_layout, this);

【讨论】:

  • 有趣!我敢打赌这种情况经常发生。
  • 我一直认为将自定义 CardView 包装在 FrameLayout 中是解决此问题的唯一方法,很高兴知道这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-06
  • 1970-01-01
  • 2021-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多