[英]How to override standart padding in CardView while using cardUseCompatPadding?
[英]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>
我的形象如下。 這是不好的,因為缺少第二張卡的海拔高度。
由於我也希望在第二張卡片視圖中顯示高程,因此,如下所示,為我的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"
card_view:cardUseCompatPadding="true"
。 並且視圖如下。
<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仍被某些東西包裹(在本例中為LinearLayout)。 保證金和cardUseCompatPadding無法解決我的問題。
所以我的問題是,如何在不使用margin或cardUseCompatPadding的情況下保留CardView(由LinearLayout包裝)高程,並且看起來仍然與CardView完全相同(僅包含)。 (即我圖表的第一張卡片視圖)
P / S:請勿要求我刪除LinearLayout,因為它僅用於說明。 我出於其他原因需要該包裝器(即,動態更改其中的視圖)
試試這個,對您有幫助。
這是您的第一個代碼,我為第二個include標簽添加了寬度和高度。
<?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>
和Cardview布局
<?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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.