如何在不使用边距或 cardUseCompatPadding 的情况下保留 CardView(由 LinearLayout 包裹)高度?
How to preserved the CardView (wrap by LinearLayout) elevation, without using margin or cardUseCompatPadding?
我有以下 xml 布局,其中有两个卡片视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.elye.myapplication.MainActivity">
<include layout="@layout/card_layout"
android:layout_margin="16dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<LinearLayout
android:layout_margin="16dp"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<include layout="@layout/card_layout"/>
</LinearLayout>
</LinearLayout>
看卡片就这么简单
<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:id="@+id/card_new_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="10dp">
<TextView
android:layout_gravity="center"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="MY TEXT"
android:layout_width="wrap_content" />
</android.support.v7.widget.CardView>
我的图片如下。这不太好,因为缺少第二张卡片的高度。
因为我也想在我的第二个卡片视图中显示高度,所以我为我的卡片视图添加了 android:layout_margin="10dp"
,如下所示。请注意 10dp 以更清楚地表明现在两个 cardview 的大小不同。
<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:id="@+id/card_new_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:clickable="true"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="10dp">
<TextView
android:layout_gravity="center"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="MY TEXT"
android:layout_width="wrap_content" />
</android.support.v7.widget.CardView>
现在的视图如下所示。这不好。我的第二张卡缩水了。
所以现在我用 card_view:cardUseCompatPadding="true"
删除了 android:layout_margin="10dp"
。以及如下视图。
<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:id="@+id/card_new_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true"
android:clickable="true"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="10dp">
<TextView
android:layout_gravity="center"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="MY TEXT"
android:layout_width="wrap_content" />
</android.support.v7.widget.CardView>
现在两者都有高度并且尺寸一致。但是后来它在两个 cardview 之间有一个极端的巨大差距,这对我不利。我想要我原来的缝隙...按照底部的图片。
理想情况下,我想要如下所示的内容,第二个 cardView 仍然被某些内容(在本例中为 LinearLayout)包裹。保证金和 cardUseCompatPadding 没有解决我的问题。
所以我的问题是,如何在不使用边距或 cardUseCompatPadding 的情况下保留 CardView(由 LinearLayout 包裹)高度,并且仅通过纯粹包含看起来仍然与 CardView 相同。 (即我图表的第一个卡片视图)
P/S:不要让我删除LinearLayout,因为它被用作插图。我出于其他原因需要该包装器(即动态更改其中的视图)
试试这个,这会对你有所帮助。
这是你的第一个代码,我已经为第二个包含标签添加了宽度和高度。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.elye.myapplication.MainActivity">
<include
layout="@layout/card_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp" />
<LinearLayout
android:id="@+id/card_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--<include-->
<!--layout="@layout/card_layout"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_margin="16dp" />-->
</LinearLayout>
</LinearLayout>
和卡片视图布局
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/card_new_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:text="MY TEXT" />
</android.support.v7.widget.CardView>
Activity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
try {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.card_layout);
final LayoutInflater factory = getLayoutInflater();
View view = factory.inflate(R.layout.card_layout, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int x = (int) getResources().getDimension(R.dimen.margin_16);
params.setMargins(x, x, x, x);
view.setLayoutParams(params);
linearLayout.addView(view);
} catch (Exception e) {
e.printStackTrace();
}
}
在res/values/dimens.xml中添加以下行
<dimen name="margin_16">16dp</dimen>
我有以下 xml 布局,其中有两个卡片视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.elye.myapplication.MainActivity">
<include layout="@layout/card_layout"
android:layout_margin="16dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<LinearLayout
android:layout_margin="16dp"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<include layout="@layout/card_layout"/>
</LinearLayout>
</LinearLayout>
看卡片就这么简单
<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:id="@+id/card_new_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="10dp">
<TextView
android:layout_gravity="center"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="MY TEXT"
android:layout_width="wrap_content" />
</android.support.v7.widget.CardView>
我的图片如下。这不太好,因为缺少第二张卡片的高度。
因为我也想在我的第二个卡片视图中显示高度,所以我为我的卡片视图添加了 android:layout_margin="10dp"
,如下所示。请注意 10dp 以更清楚地表明现在两个 cardview 的大小不同。
<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:id="@+id/card_new_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:clickable="true"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="10dp">
<TextView
android:layout_gravity="center"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="MY TEXT"
android:layout_width="wrap_content" />
</android.support.v7.widget.CardView>
现在的视图如下所示。这不好。我的第二张卡缩水了。
所以现在我用 card_view:cardUseCompatPadding="true"
删除了 android:layout_margin="10dp"
。以及如下视图。
<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:id="@+id/card_new_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true"
android:clickable="true"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="10dp">
<TextView
android:layout_gravity="center"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="MY TEXT"
android:layout_width="wrap_content" />
</android.support.v7.widget.CardView>
现在两者都有高度并且尺寸一致。但是后来它在两个 cardview 之间有一个极端的巨大差距,这对我不利。我想要我原来的缝隙...按照底部的图片。
理想情况下,我想要如下所示的内容,第二个 cardView 仍然被某些内容(在本例中为 LinearLayout)包裹。保证金和 cardUseCompatPadding 没有解决我的问题。
所以我的问题是,如何在不使用边距或 cardUseCompatPadding 的情况下保留 CardView(由 LinearLayout 包裹)高度,并且仅通过纯粹包含看起来仍然与 CardView 相同。 (即我图表的第一个卡片视图)
P/S:不要让我删除LinearLayout,因为它被用作插图。我出于其他原因需要该包装器(即动态更改其中的视图)
试试这个,这会对你有所帮助。
这是你的第一个代码,我已经为第二个包含标签添加了宽度和高度。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.elye.myapplication.MainActivity">
<include
layout="@layout/card_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp" />
<LinearLayout
android:id="@+id/card_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--<include-->
<!--layout="@layout/card_layout"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_margin="16dp" />-->
</LinearLayout>
</LinearLayout>
和卡片视图布局
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/card_new_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:text="MY TEXT" />
</android.support.v7.widget.CardView>
Activity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
try {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.card_layout);
final LayoutInflater factory = getLayoutInflater();
View view = factory.inflate(R.layout.card_layout, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int x = (int) getResources().getDimension(R.dimen.margin_16);
params.setMargins(x, x, x, x);
view.setLayoutParams(params);
linearLayout.addView(view);
} catch (Exception e) {
e.printStackTrace();
}
}
在res/values/dimens.xml中添加以下行
<dimen name="margin_16">16dp</dimen>