Geo*_*ter 29 xml android android-cardview
我是Android新手,这是我的第一个问题.
我想在cardview的开头添加彩色垂直边框.我怎样才能在xml上实现它?我尝试用空textview添加它,但它搞乱了整个cardview本身.请查看下面发布的图片链接.
activity_main.xml中
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:contentPadding="16dp"
card_view:cardElevation="2dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
style="@style/Base.TextAppearance.AppCompat.Headline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title" />
<TextView
style="@style/Base.TextAppearance.AppCompat.Body1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Content here" />
</LinearLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
非常感谢
Aun*_*win 59
从材料设计更新开始,它也支持app:strokeColor
和app:strokeWidth
。看更多
使用材料设计更新。将以下代码添加到build.gradle
(:app)
dependencies {
// ...
implementation 'com.google.android.material:material:1.0.0'
// ...
}
Run Code Online (Sandbox Code Playgroud)
小智 37
由于接受的答案要求您添加框架布局,这里介绍如何使用材料设计来做到这一点。
如果您还没有添加此内容,请添加
implementation 'com.google.android.material:material:1.0.0'
Run Code Online (Sandbox Code Playgroud)
现在将 Cardview 更改为 MaterialCardView
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="2dp"
app:strokeWidth="1dp"
app:strokeColor="@color/black">
Run Code Online (Sandbox Code Playgroud)
现在您需要将活动主题更改为 Theme.Material。如果您正在使用 Theme.Appcompact,我建议您在未来的项目中转向 Theme.Material,以便在您的应用程序中拥有更好的材料设计。
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
Run Code Online (Sandbox Code Playgroud)
kan*_*idj 32
尝试做:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardElevation="2dp"
card_view:cardCornerRadius="5dp">
<FrameLayout
android:background="#FF0000"
android:layout_width="4dp"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="vertical">
<TextView
style="@style/Base.TextAppearance.AppCompat.Headline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title" />
<TextView
style="@style/Base.TextAppearance.AppCompat.Body1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Content here" />
</LinearLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
这将从cardview中删除填充并添加带有颜色的FrameLayout.然后,您需要在LinearLayout中修复填充,然后修复其他字段
更新
如果要保留卡片角半径,请在drawable文件夹中创建card_edge.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#F00" />
<size android:width="10dp"/>
<padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"/>
<corners android:topLeftRadius="5dp" android:bottomLeftRadius="5dp"
android:topRightRadius="0.1dp" android:bottomRightRadius="0.1dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
并在框架布局中使用 android:background="@drawable/card_edge"
Ami*_*mit 12
我认为这种解决方案效率可能不高,但它可以达到目的,并增加了边框宽度的灵活性.
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="40dp"
android:layout_gravity="center"
card_view:cardBackgroundColor="@color/some_color"
card_view:cardCornerRadius="20dp"
card_view:contentPadding="5dp"> <!-- Change it to customize the border width -->
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
card_view:cardCornerRadius="20dp"
card_view:contentPadding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Add your UI elements -->
</RelativeLayout>
</android.support.v7.widget.CardView>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
我的解决方案:
创建文件card_view_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white_background"/>
<stroke android:width="2dp"
android:color="@color/red" />
<corners android:radius="20dip"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
并以编程方式设置
cardView.setBackgroundResource(R.drawable.card_view_border);
Run Code Online (Sandbox Code Playgroud)
CardView
扩展 FrameLayout,因此它支持foreground
属性。使用foreground
属性还可以轻松添加边框。
布局如下:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/link_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="@drawable/bg_roundrect_ripple_light_border"
app:cardCornerRadius="23dp"
app:cardElevation="0dp">
</androidx.cardview.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
bg_roundrect_ripple_light_border.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/ripple_color_light">
<item>
<shape android:shape="rectangle">
<stroke
android:width="0.5dp"
android:color="#DDDDDD" />
<corners android:radius="23dp" />
</shape>
</item>
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<corners android:radius="23dp" />
<solid android:color="@color/background" />
</shape>
</item>
</ripple>
Run Code Online (Sandbox Code Playgroud)
我想改进 Amit 提出的解决方案。我正在使用给定的资源而不添加额外的shapes
或Views
. 我给CardView
了一个背景色,然后嵌套布局,白色叠印还有一些leftMargin
......
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardElevation="2dp"
card_view:cardBackgroundColor="@color/some_color"
card_view:cardCornerRadius="5dp">
<!-- The left margin decides the width of the border -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_marginLeft="5dp"
android:background="#fff"
android:orientation="vertical">
<TextView
style="@style/Base.TextAppearance.AppCompat.Headline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title" />
<TextView
style="@style/Base.TextAppearance.AppCompat.Body1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Content here" />
</LinearLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
81457 次 |
最近记录: |