Pun*_*wah 36 android android-layout android-cardview
我想只在卡片顶部转角.
我使用下面的属性,它正在四处转弯.
我想显示所有卡片的重叠
card_view:cardCornerRadius="4dp"
Run Code Online (Sandbox Code Playgroud)
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp"
card_view:cardPreventCornerOverlap="false"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/re1">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="@color/colorAccent"
android:text="contact det"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:textSize="14dp"/>
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="@id/title"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"/>
<TextView
android:id="@+id/txtSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="@id/txtName"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="@+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="10dp"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_marginRight="150dp"
android:layout_alignBaseline="@id/txtName"/>
<TextView
android:id="@+id/txtAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textSize="10dp"
android:layout_alignLeft="@id/txtEmail"
android:layout_alignBaseline="@id/txtSurname"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
Swa*_*thi 44
我们可以将卡片视图的marginBottom设置为负值.Margin应该与卡片半径相同.例如,
<FrameLayout
android:id="@+id/rootview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_marginBottom="-3dp"
project:cardCornerRadius="3dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--The child view inside the cardview should have extra padding,so that negative margin will not affect the bottom padding of its child.Here normally we have 16dp bottom padding for child + margin bottom of the parent is 3dp=19dp comes.-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="19dp"/>
</android.support.v7.widget.CardView>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
它对我有用.但我怀疑这是否是正确的做法.欢迎提出任何建议.
Ano*_*p M 13
如果您只想塑造图像形状,请使用ShapeableImageView使用提供的形状绘制位图的材质。
<!-- Media -->
<com.google.android.material.imageview.ShapeableImageView
...
app:shapeAppearance="?attr/shapeAppearanceMediumComponent"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.Card.Media" />
Run Code Online (Sandbox Code Playgroud)
在res/values/styles.xml:
<style name="ShapeAppearanceOverlay.App.Card.Media" parent="">
<item name="cornerSizeBottomLeft">0dp</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerSizeTopLeft">8dp</item>
<item name="cornerSizeTopRight">8dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我一直在尝试相同的方法,但是没有提供的解决方案对我有用。
唯一有效的是:
1)制作一个带有圆角的自定义背景资源(如矩形)。
2)使用命令设置此自定义背景-
cardView = view.findViewById(R.id.card_view2);
cardView.setBackgroundResource(R.drawable.card_view_bg);
Run Code Online (Sandbox Code Playgroud)
为我完美地工作!希望这对您有所帮助。
我使用左上角和右下角半径进行的XML布局。
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<corners android:topLeftRadius="18dp" android:bottomRightRadius="18dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
在您的情况下,您只需要更改topLeftRadius和topRightRadius。
棘手的事情是因为您无法让CardView做到这一点。在内部,它使用RoundRectDrawable(私有包)的用法roundRect如下:
Run Code Online (Sandbox Code Playgroud)// rectf, rx, ry, paint canvas.drawRoundRect(mBoundsF, mRadius, mRadius, paint);
因此,您需要一个不同的解决方案,例如,我发现Ahmed-Abdelmeged的要点是,他们在每个角落使用画布剪辑,并使用一条路径描述轮廓。
因此,尽管我不是编写此代码的人,但我会将其发布在这里,以供将来的旅行者使用。
Run Code Online (Sandbox Code Playgroud)<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="RoundedView"> <attr name="topLeftCornerRadius" format="dimension" /> <attr name="topRightCornerRadius" format="dimension" /> <attr name="bottomLeftCornerRadius" format="dimension" /> <attr name="bottomRightCornerRadius" format="dimension" /> </declare-styleable> </resources>
和
Run Code Online (Sandbox Code Playgroud)package com.abdelmeged.ahmed.roundedlayout; /** * Created by ahmed on 9/17/2017. */ import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Path; import android.graphics.RectF; import android.graphics.Region; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; import android.widget.FrameLayout; /** * Custom wrapper view to get round corner round view */ public class RoundedView extends FrameLayout { /** * The corners than can be changed */ private float topLeftCornerRadius; private float topRightCornerRadius; private float bottomLeftCornerRadius; private float bottomRightCornerRadius; public RoundedView(@NonNull Context context) { super(context); init(context, null, 0); } public RoundedView(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context, attrs, 0); } public RoundedView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs, defStyleAttr); } private void init(Context context, AttributeSet attrs, int defStyle) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundedView, 0, 0); //get the default value form the attrs topLeftCornerRadius = typedArray.getDimension(R.styleable. RoundedView_topLeftCornerRadius, 0); topRightCornerRadius = typedArray.getDimension(R.styleable. RoundedView_topRightCornerRadius, 0); bottomLeftCornerRadius = typedArray.getDimension(R.styleable. RoundedView_bottomLeftCornerRadius, 0); bottomRightCornerRadius = typedArray.getDimension(R.styleable. RoundedView_bottomRightCornerRadius, 0); typedArray.recycle(); setLayerType(View.LAYER_TYPE_SOFTWARE, null); } @Override protected void dispatchDraw(Canvas canvas) { int count = canvas.save(); final Path path = new Path(); float[] cornerDimensions = { topLeftCornerRadius, topLeftCornerRadius, topRightCornerRadius, topRightCornerRadius, bottomRightCornerRadius, bottomRightCornerRadius, bottomLeftCornerRadius, bottomLeftCornerRadius}; path.addRoundRect(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()) , cornerDimensions, Path.Direction.CW); canvas.clipPath(path); super.dispatchDraw(canvas); canvas.restoreToCount(count); } public void setTopLeftCornerRadius(float topLeftCornerRadius) { this.topLeftCornerRadius = topLeftCornerRadius; invalidate(); } public void setTopRightCornerRadius(float topRightCornerRadius) { this.topRightCornerRadius = topRightCornerRadius; invalidate(); } public void setBottomLeftCornerRadius(float bottomLeftCornerRadius) { this.bottomLeftCornerRadius = bottomLeftCornerRadius; invalidate(); } public void setBottomRightCornerRadius(float bottomRightCornerRadius) { this.bottomRightCornerRadius = bottomRightCornerRadius; invalidate(); } }
这样一来,您就可以在渲染图像和视图之前裁剪图像和视图的边缘,因此可以完全满足您的需求。
或者,您可以使用MaterialCardView
<com.google.android.material.card.MaterialCardView
style="@style/CustomCardViewStyle"
...>
</com.google.android.material.card.MaterialCardView>
<style name="CustomCardViewStyle" parent="@style/Widget.MaterialComponents.CardView">
<item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay_card_custom_corners</item>
</style>
<style name="ShapeAppearanceOverlay_card_custom_corners" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">4dp</item>
<item name="cornerSizeTopLeft">8dp</item>
<item name="cornerSizeBottomRight">16dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
你可以在这里找到一个很好的答案
| 归档时间: |
|
| 查看次数: |
31312 次 |
| 最近记录: |