【问题标题】:set background png on Linearlayout without affecting layout size在Linearlayout上设置背景png而不影响布局大小
【发布时间】:2018-07-31 14:38:11
【问题描述】:

背景图像不填充线性布局并更改该大小

  <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="@mipmap/dashboard_background"
      android:orientation="vertical"
      android:padding="10dp">

好看的是这个

我怎样才能做到这一点?

【问题讨论】:

    标签: java android android-layout kotlin android-linearlayout


    【解决方案1】:

    您应该使用带有图像视图和内部线性布局的框架布局

    <FrameLayout
    android:id="@+id/FrameLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <ImageView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:src="@drawable/background"/>
    
    <LinearLayout
        ---Your Layout---
    </LinearLayout>
    

    【讨论】:

    • 它可以按我的意愿工作,但是我的布局中只有一些元素是不可见的,你知道为什么吗?
    • 隐形是什么意思?我的猜测是,由于明亮的背景,您的白色文本几乎看不到。你能确认一下吗?
    • 没有底部的元素不可见i.prntscr.com/7uuWI1bUQmeozpj_YWQ-lg.png
    • 你能提供我当前的 xml 吗?
    • 当然是的 codeshare.io/ado43B LifetimeStats 从 LinearLayout 扩展
    【解决方案2】:

    背景始终延伸到其View 大小。所以你无法真正控制它的外观。相反,将ImageView 作为布局中最底部的元素(意味着它将位于其他所有元素之下),并使用您的背景图像作为此ImageView 的源(src),这样您将能够使用scaleType 控制背景的外观(您可能想要使用centerCropfitCenter)。更多关于scaleTypehere

    最后你的布局应该是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent">
    
      <ImageView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:scaleType="fitCenter"
          android:src="@mipmap/dashboard_background" />
    
      <FrameLayout
          android:id="@+id/content_container"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    
        <!-- Put your view elements here -->
    
      </FrameLayout>
    
    </FrameLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      相关资源
      最近更新 更多