【问题标题】:Views not wrapping content inside Card View视图没有在卡片视图中包装内容
【发布时间】:2020-09-11 07:25:51
【问题描述】:

在 Android L 的 CardView 中使用时,某些视图不会包装其内容。在早期版本的 Android 上测试相同的代码时,一切正常。这是我的布局:

<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:layout_width="match_parent"
android:layout_height="@dimen/stock_row_height"
card_view:cardCornerRadius="@dimen/default_elevation"
card_view:cardElevation="@dimen/default_elevation"
card_view:cardUseCompatPadding="true">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="@dimen/portals_logo_width"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_margin="@dimen/portals_logo_margin"
        android:adjustViewBounds="true"
        android:src="@drawable/image_placeholder" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/logo"
        android:layout_toRightOf="@+id/logo"
        android:background="@android:color/black"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_green_light"
            android:text="portalAddress.com"
            android:textColor="@android:color/black"
            android:textSize="@dimen/portals_title_text_size" />

        <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_light"
            android:text="Sincronizado 04/12/14 às 14:25"
            android:textColor="@color/medium_gray"
            android:textSize="@dimen/portals_description_text_size" />

    </LinearLayout>

</RelativeLayout>

问题是高度的 TextView "wrap_content" 不起作用。这是来自 Android Studio 的预览可视化:

Android Studio Preview

这是在搭载 Android L 的三星 Galaxy S5 上运行的相同布局:

Samsung Galaxy S5 with Android L

如果我尝试为两个 TextView(分别为 25dp 和 18dp)设置固定高度,则父 LinearLayout 不会以与 TextView 相同的方式包装其内容。

[TextView 的固定高度][3]

我错过了什么?不知道是什么问题,因为这只发生在 Android L 中。

编辑:我仍在寻找解决此问题的方法。

【问题讨论】:

  • 我没有足够的声望来发布 3 个链接,所以这里是最后一个的地址 - Fixed height for TextView
  • 您找到解决方案了吗?我也有同样的问题。

标签: android android-layout textview android-cardview


【解决方案1】:

将文本视图宽度设置为 wrap_content 而不是 match_parent 可能会起作用。您也可以在toEndOftoRightOf 属性中删除@+id/logo 中的“+”。

【讨论】:

  • 已经尝试过了,但没有成功。宽度属性工作正常,但高度没有考虑“wrap_content”。感谢“+”号的提示。
  • 你能告诉我你的S5上安装的L的版本号是20还是21还是22?以及您使用的是哪个版本的支持库? (我假设 api 20,因为你提到了 Android L)
  • 我正在使用支持版本 22.1.0、CompileSdkVersion 22、buildToolsVersion 22.0.1 和 targetSdkVersion 22。S5 android 版本是 5.0。
【解决方案2】:

尝试从 LinearLayout 中删除 android:layout_toEndOf="@+id/logo",因为它将此视图的开始边缘定位到徽标 ImageView 的末尾。

【讨论】:

    【解决方案3】:

    有时背景/src 图像是导致此问题的原因,请尝试使用较小的图像并将它们转换为 9P 的图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      • 2016-02-04
      • 2014-06-15
      相关资源
      最近更新 更多