【问题标题】:Android ImageView doesn't wrap contentAndroid ImageView 不包装内容
【发布时间】:2016-03-02 05:49:00
【问题描述】:

无法解决 TableLayout 内的 ImageView 问题。
我将layout_widthlayout_height 都设置为wrap_content
但是左右两侧出现了一些不需要的区域。
(图片实际上是方形的)

探索了类似的问题
这里 Why do I need to wrap an ImageView into a FrameLayout?
这里 LinearLayout not wrapping content 和其他人

试过

  • 设置adjustViewBounds="true",
  • 在 dp 中设置固定宽度和高度,
  • 玩过scalTypes
  • 包裹在FrameLayout中,

但对这个问题没有任何帮助。
为什么会出现,如何摆脱它并只包装图像?

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="8dp"
    android:background="@android:drawable/dialog_holo_light_frame"
    android:padding="20dp" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/tv_deal_number_description"
                style="@style/ListHeadingTextStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/deal_number"
                 />

            <TextView
                android:id="@+id/tv_deal_number"
                style="@style/ListHeadingTextStyle"
                android:textColor="@color/accent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:maxLines="1" />
        </LinearLayout>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <View
            android:id="@+id/div_under_deal_number"
            style="@style/Divider"
            android:layout_marginBottom="6dp"
            android:layout_marginTop="6dp"
            android:layout_weight="1" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/descriptions_vertical_margin"
        android:gravity="center" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/ic_margin_right"
            android:layout_marginRight="@dimen/ic_margin_right"
            android:contentDescription="@null"
            android:gravity="start"
            android:src="@drawable/ic_person_grey600_24dp"
            android:adjustViewBounds="true"

           />

        <TextView
            android:id="@+id/tv_client_name_description"
            style="@style/DescriptionTextStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/description_value_horizontal_margin"
            android:layout_marginRight="@dimen/description_value_horizontal_margin"
            android:text="@string/client_name_col" />

        <TextView
            android:id="@+id/tv_client_name"
            style="@style/ListValuesTextStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:maxLines="2" />
    </TableRow>
...

【问题讨论】:

    标签: android imageview tablelayout


    【解决方案1】:

    虽然您设置了尺寸 wrap_content,但您也设置了边距。

    android:layout_marginEnd="@dimen/ic_margin_right"
    android:layout_marginRight="@dimen/ic_margin_right"
    

    尝试删除这些arrributes或将它们设置为0。

    【讨论】:

    • 如您所见,此边距仅在右侧。删除它们也无济于事。
    • 可能与 TableLayout 有某种关系?
    【解决方案2】:

    实际上,这是由于您的形象造成的。尝试将您的图像裁剪为合适的。即使图像也是透明的,您的图像在左侧和右侧也有额外的空间。重试会有帮助。

    【讨论】:

      【解决方案3】:

      由于标题而出现问题。
      从表格的第一行删除标题后,ImageViews 开始没有边距,只是包装了内容 (老实说,我不知道为什么)。
      所以我只是把标题放在表格布局之外并达到了预期:

      现在代码如下:

         <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_marginTop="8dp"
          android:background="@android:drawable/dialog_holo_light_frame"
          android:orientation="vertical"
          android:padding="20dp" >
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center_horizontal"
              android:orientation="horizontal" >
      
              <TextView
                  android:id="@+id/tv_deal_number_description"
                  style="@style/ListHeadingTextStyle"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/deal_number" />
      
              <TextView
                  android:id="@+id/tv_deal_number"
                  style="@style/ListHeadingTextStyle"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:maxLines="1"
                  android:textColor="@color/accent" />
          </LinearLayout>
      
          <TableLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent" >
      
              <TableRow
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" >
      
                  <View
                      android:id="@+id/div_under_deal_number"
                      style="@style/Divider"
                      android:layout_marginBottom="6dp"
                      android:layout_marginTop="6dp"
                      android:layout_weight="1" />
              </TableRow>
      
              <TableRow
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="@dimen/descriptions_vertical_margin"
                  android:gravity="center" >
      
                  <ImageView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginEnd="@dimen/ic_margin_right"
                      android:layout_marginRight="@dimen/ic_margin_right"
                      android:adjustViewBounds="true"
                      android:contentDescription="@null"
                      android:gravity="start"
                      android:src="@drawable/ic_person_grey600_24dp" />
      
                  <TextView
                      android:id="@+id/tv_client_name_description"
                      style="@style/DescriptionTextStyle"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginEnd="@dimen/description_value_horizontal_margin"
                      android:layout_marginRight="@dimen/description_value_horizontal_margin"
                      android:text="@string/client_name_col" />
      
                  <TextView
                      android:id="@+id/tv_client_name"
                      style="@style/ListValuesTextStyle"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_weight="1"
                      android:maxLines="2" />
              </TableRow>
          ...
      

      【讨论】:

        猜你喜欢
        • 2013-08-07
        • 2012-08-28
        • 1970-01-01
        • 1970-01-01
        • 2019-12-06
        • 2021-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多