【问题标题】:How to remove extra padding of "CardView" android如何删除“CardView”android的额外填充
【发布时间】:2018-03-14 15:53:21
【问题描述】:

我的布局中有一个 CardView,宽度为 match_parent。当我在我的 Marshmallow 测试设备中运行该应用程序时,它可以完美运行。但在我的 Jellybean 设备中,它显示了一些填充。

我试过了

app:cardPreventCornerOverlap="false" app:cardUseCompatPadding="false" android:hardwareAccelerated="true"

但它不起作用。

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wiinnova.cryptcoin">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:name=".activities.ThisApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".activities.HomeActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.ContainerActivity"
            android:windowSoftInputMode="stateAlwaysHidden" />
        <activity
            android:name=".activities.TransactionActivity"
            android:label="@string/title_activity_transaction"
            android:theme="@style/AppTheme"></activity>
    </application>
</manifest>

片段布局:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f00"
    android:orientation="vertical"
    tools:context="com.wiinnova.cryptcoin.fragments.CoinsList_Frg">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark"
        android:orientation="vertical">

        <ProgressBar
            android:id="@+id/pro_bar"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:foregroundGravity="center"
            android:indeterminate="true"
            android:visibility="gone" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="#f00">

            <android.support.v7.widget.CardView
                android:id="@+id/main_top_card"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:hardwareAccelerated="true"
                app:cardElevation="10dp"
                app:cardPreventCornerOverlap="false"
                app:cardUseCompatPadding="false">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/top_card_img"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:padding="20dp">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Watchlist"
                        android:textColor="#fff"
                        android:textSize="25sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Evolution : Current"
                        android:textColor="#fff" />

                </LinearLayout>

            </android.support.v7.widget.CardView>

        </LinearLayout>

        <com.baoyz.widget.PullRefreshLayout
            android:id="@+id/coin_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:refreshColor="@color/colorPrimaryDark"
            app:refreshType="material">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/coins_list_recycler"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </com.baoyz.widget.PullRefreshLayout>

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/add_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:src="@drawable/ic_add"
        app:elevation="10dp"
        app:layout_anchor="@id/main_top_card"
        app:layout_anchorGravity="bottom|right|end" />

</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 分享您的清单文件。
  • 我想要一个本地解决方案。不是图书馆。
  • 已添加清单。
  • 在你的应用标签中添加android:hardwareAccelerated="true"这一行。

标签: android android-cardview


【解决方案1】:

在你的 xml 中减少你的 cardview 高度大小

【讨论】:

  • 我想提升我的应用程序。
  • 移除高程是可行的,但不完全是 2dp 填充。
【解决方案2】:

您为所有设备和平板电脑使用了一个库来管理 UI 设计。

在 gradle 文件中添加以下依赖项...

compile 'com.intuit.sdp:sdp-android:1.0.4'

然后在定义任何填充后,以下面的代码方式进行边距..

        android:padding="@dimen/_20sdp"
        android:layout_marginTop="@dimen/_5sdp"

【讨论】:

  • 我不想要“填充”,但是使用棒棒糖前设备时会自动添加
  • 使用上面的库来定义任何填充或边距,然后任何设备不采用任何类型的自动填充.. 然后在下面使用,如棒棒糖前设备中的卡片视图问题:- github.com/nanofaroque/ExtraPaddingRemoveFromCardView