【问题标题】:Set a bitmap as background and crop it将位图设置为背景并裁剪
【发布时间】:2018-09-11 11:01:31
【问题描述】:

我正在尝试将位图图像设置为 FrameLayout 的背景。

<FrameLayout
    android:id="@+id/game_list_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>

    <LinearLayout
        android:id="@+id/game_list_header_cnt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/game_list_header_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/game_list_header_sub_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    </LinearLayout>
</FrameLayout>

问题在于我想设置为背景的图像的高度大于两个 TextView 的高度,因此game_list_header 的高度不适合 TextView 的高度,而是 ImageView 的高度。

我尝试了不同的方法,例如将位图用作带有gravity=clip_vertical|fill_horizontal 的 DrawableBitmap,但即使这样,图像的高度也不适合 TextView 的高度,导致 FrameLayout 比我想要的大。

【问题讨论】:

    标签: android android-layout imageview android-imageview crop


    【解决方案1】:

    您是否尝试将图像设置为game_list_header_cnt 的背景?

    如果这没有帮助,您可以简单地使用 ConstraintLayout 并制作 imageView top_top 的顶部 textViewbot_bot 的机器人 textView

    【讨论】:

    • 是的,我已经尝试了您的第一个建议,但结果并不好。但我会给 ConstraintLayout 一个机会。谢谢
    • 不要忘记在 imageView 上使用 scaleType 属性
    • 是的,ConstraintLayout 完成了这项工作。我不得不将 ImageView 高度更改为 wrap_content
    【解决方案2】:

    FrameLayout 的高度设置为,可能是 40dp。由于您已将 FrameLayout 的高度设置为 wrap_content,因此 XML 也会考虑图像的高度。

    【讨论】:

    • 确定使用固定高度可以解决问题,但是我不能使用固定高度,因为我不知道TextView的高度而且会经常变化
    【解决方案3】:

    TextViewlayout_height设置为wrap_content,所以TextView的实际高度由TextView的textsize决定。

    您可以将layout_height of FrameLayout 设置为一个数字,例如80dp,并将layout_height of LinearLayout 设置为match_parent

    【讨论】:

    • 确定使用固定高度可以解决问题,但是我不能使用固定高度,因为我不知道TextView的高度而且会经常变化
    • 我明白了,constraintlayout 可以适合你,等几分钟,我会测试并发布更新的答案。
    • 谢谢,我使用了一个 ConstraintLayout,正如 Max Aves 之前建议的那样,它可以工作
    猜你喜欢
    • 2015-11-13
    • 2012-12-02
    • 2016-06-06
    • 2017-02-26
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    相关资源
    最近更新 更多