【问题标题】:Android LinearLayout background ImageAndroid LinearLayout 背景图片
【发布时间】:2018-11-14 15:02:29
【问题描述】:

如果我尝试将图像显示为背景,我的视图会出现问题,它占用了整个空间,并且线性布局的第二个子项(2 个彼此相邻的 RelativeLayout)不会出现在视图中。

如何将图像设置为背景,保持其他布局元素在其上方?

<?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:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/globe"
        android:layout_gravity="top"
        />

    </RelativeLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


      <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" >
        </RelativeLayout>


      <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" >
        </RelativeLayout>

    </LinearLayout>


</LinearLayout>

【问题讨论】:

    标签: android android-linearlayout android-relativelayout


    【解决方案1】:

    有点晚了,但是我的问题已经用下面的代码解决了。

    使用ImageView 设置我的背景图片,然后在其上显示线性布局。

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:contentDescription="@string/my_msg"
            android:src="@drawable/my_super_bg_image" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/top_style"
            android:orientation="vertical" >
    
            <RelativeLayout
                android:id="@+id/today"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:paddingRight="5dp" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:text="some text here"
                    android:textSize="24sp" />
            </RelativeLayout>
    
        </LinearLayout>
    
    </FrameLayout>
    

    如果有人需要了解更多,我会更新答案。

    谢谢。

    【讨论】:

    • iv 不应该有匹配的父级,例如 fitxy 用于案例 ll 布局比图像可绘制 bg 更大?
    • 就我而言,我还需要为 ImageView 添加android:scaleType="centerCrop"
    【解决方案2】:

    在您的LinearLayout 中使用android:background

    【讨论】:

    • 我发现这个答案对于那些有特定分辨率并且可以进行一些图像编辑的人仍然有帮助
    【解决方案3】:

    您必须使用android:background 属性而不是ImageView

    【讨论】:

    • 正如在其他地方提到的,当使用此属性时,图像会被拉伸和挤压以适应设备屏幕。我希望图像居中而不是拉伸和挤压。
    猜你喜欢
    • 2013-12-03
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多