问题
I have a custom control that extends CardView. I am adding it to a linear layout so that I can create a grid of cards. I'm not using a ListView or RecyclerView.
I wanted to set up a gap between the cards within the linear layout, so I defined a margin.
THe card layout is using the dark theme. My application is using the default material theme (dark). I'm testing on a 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>
I'm adding them to a list view as such:
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);
The class that uses the layout with the CardView actually extends CardView itself, e.g.
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);
}
What I see is this. The card content part looks fine.. the dark color. But the margin/gap around the CardView is white. How do I get this to be transparent, and why does the dark card theme behave this way? Also, what will the shadow look like on a dark theme like this?
回答1:
So I had a layout file which included the <CardView>
as noted above. My custom class which inflated this layout extended CardView. So, I had a CardView within a CardView. The "outer" card view, that is, the one that my custom class extended, never had its theme set, so it used the default "light" theme.
Because I was creating this widget progrmatically, I thought I needed to extend CardView. this was wrong.
Before:
public class MyCustomWidget extends CardView
After:
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);
来源:https://stackoverflow.com/questions/35800771/white-border-around-dark-themed-cardviews