【问题标题】:Android: Frame Layout - ImageView height won't fitAndroid:框架布局 - ImageView 高度不适合
【发布时间】:2017-08-21 19:16:01
【问题描述】:

我有一个框架布局,里面有一个 ImageView 和一个 TextView。我打算在图片的中心制作那个文字。 问题是图像不适合,ImageView 不会很好地包裹内容的高度。看图片:

我需要它看起来像这样:

我尝试混合高度和 with(包装内容并匹配父级),但图片仍然不太适合。 这是xml:

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

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:srcCompat="@drawable/tvrdjava"
            android:id="@+id/imageView3" />

        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/textView6"
            android:textAlignment="center"
            android:textColor="@android:color/white"
            />

</FrameLayout>

图片位于不同的可绘制文件夹中,用于不同的分辨率(hdpi、mdpi、xhdpi、xxhdpi、xxxhdpi)。

【问题讨论】:

  • 为什么不对 ImageView 和 TextView 使用 RelativeLayout?
  • 你可以使用 android:background="@drawable/your_image ..
  • 因为我不能把文字放在图片上。 @MatPag
  • android:background= ... 出于某种原因拉伸图像。 @RavindraKushwaha

标签: android android-layout android-framelayout


【解决方案1】:

您应该尝试使用类似的方法,改用相对布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        app:srcCompat="@drawable/ok"/>

    <TextView
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        android:text="TextView"/>

</RelativeLayout>

【讨论】:

  • 这种工作。此框架布局位于 LinearLayout(垂直)中,并且出于某种原因可以正常工作。无论如何,谢谢你!
【解决方案2】:

感谢@MatPag 的想法。将此框架布局放入 LinearLayout(垂直)中有效。

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

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/tvrdjava"
            android:id="@+id/imageView8"
            android:adjustViewBounds="true"
            android:cropToPadding="false" />

        <TextView
            android:text="TrTemp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/textView3"
            android:textColor="@android:color/holo_blue_dark"
            android:textAlignment="center"
            android:gravity="center"
            />

    </FrameLayout>

【讨论】:

    【解决方案3】:

    这对我有用。

                 <FrameLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
    
                    <ImageView
                        android:id="@+id/homepage_background_imageView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:adjustViewBounds="true"
                        android:src="@drawable/background_homepage"/>
    
                </FrameLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多