我有一个简单的看法如下;
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="60dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark"
android:gravity="center">
<TextView
android:Text="Some text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.cardview.widget.CardView>
即使 LinearLayout
的高度和宽度都明确设置为匹配其父级,它也匹配 CardView
的宽度但在高度方向包裹其内容。
为什么会这样?
最佳答案
我想发表评论,但无论如何我都没有“足够的声誉”。 您是否在 gradle 文件中添加了 dependecy 'androidx.cardview:cardview:1.0.0'?你的代码对我来说工作正常,但是,我不知道你是否添加了依赖项。
关于android - layout_height ="match_parent"不按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57838536/