簡體   English   中英

將位圖設置為背景並裁剪它

[英]Set a bitmap as background and crop it

我正在嘗試將位圖圖像設置為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高度。

我嘗試了不同的方法,例如使用位圖作為DrawableBitmap,使用gravity=clip_vertical|fill_horizontal但即使這樣,圖像的高度也不適合TextView的高度,導致FrameLayout比我想要的大。

FrameLayout的高度設置為,可能是40dp 由於您已將FrameLayout的高度設置為wrap_content ,因此XML也會考慮圖像的高度。

您是否嘗試將圖像設置為game_list_header_cnt的背景?

如果沒有幫助,您可以簡單地使用ConstraintLayoutimageView top_top頂部的textViewbot_bot機器人的textView

layout_heightTextView被設置為wrap_content ,因此的實際高度TextView通過的TEXTSIZE確定TextView

你可以將FrameLayoutlayout_height設置為一個數字,例如80dp,並將LinearLayout layout_height設置為match_parent

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM