2016-03-04 121 views
3

我有一个扩展CardView的自定义控件。我将它添加到线性布局中,以便我可以创建一个网格。我没有使用ListView或RecyclerView。黑色主题周围的白色边框CardViews

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

卡片布局使用黑暗的主题。我的应用程序使用默认材质主题(黑暗)。我正在测试Android 6.0.1 Pixel C。

<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周围的空白/空白是白色的。我如何才能让它变得透明,为什么黑卡主题会以这种方式表现出来?而且,这样的黑暗主题会是什么样的影子?

enter image description here

+0

你有刚刚试过'newLinearLayout'的背景属性设置为你想要颜色? – NoChinDeluxe

+0

这样做似乎没有任何效果。这只是cardlayout周围的边缘是白色的,而不是线性布局本身。 –

回答

1

所以只好其中包括<CardView>如上所述布局文件。我的自定义类扩大了这种布局扩展CardView。所以,我在CardView中有一个CardView。 “外部”卡片视图,即我的自定义类扩展的视图,从未设置其主题,因此它使用默认的“轻量级”主题。

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

前:

public class MyCustomWidget extends CardView 

后:

public class MyCustomWidgetextends 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); 
+1

有趣!我敢打赌,这经常发生。 –