【问题标题】:How to setup background not to be stretched?如何设置背景不被拉伸?
【发布时间】:2013-02-26 17:47:04
【问题描述】:

请告知如何将图像放置为LinearLayout 的背景,以便该图像看起来像原样,而不是拉伸、标题或其他内容。

【问题讨论】:

    标签: android


    【解决方案1】:

    其他解决方案是在 XML 中定义您的图像并在您的背景上引用此 XML,如下所述:Background Image Placement
    像这样你可以控制你的图像(背景)布局

    【讨论】:

      【解决方案2】:

      如果线性布局的内容小于图像高度,Jojo 的解决方案可能会起作用。 如果我添加两个或自动编辑文本,图像开始拉伸。如果您仍然想避免并在后台使用图像视图,请使用以下 xml 布局。

      <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
          <ImageButton android:id="@+id/imageButton1" android:src="@drawable/icon" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@null"></ImageButton>
          <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="100px"></Button>
      </RelativeLayout>
      

      因为只有图像按钮的 src 选项对图像的宽度和高度保持不变。

      【讨论】:

        【解决方案3】:

        使用 .xml 文件中的此代码将 android 的 LinearLayout 的高度和宽度转换为 wrap_content

        android:layout_height="wrap_content"  
        android:layout_width="wrap_content"  
        

        为图像设置背景使用代码

        android:background="@drawable/image"
        

        现在将图像放入可绘制文件中

        【讨论】:

        • 他问如何确保图像不被拉伸。使用您的解决方案,如果线性布局的内容变得大于图像,则图像将再次被拉伸。你是怎么解决的?
        猜你喜欢
        • 2023-03-29
        • 2014-06-12
        • 1970-01-01
        • 2016-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多