Android的:如何在一个视图中居中背景图片 [英] Android: how to have a centered background image in a view

查看:1381
本文介绍了Android的:如何在一个视图中居中背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个小的(图标大小)的图像作为背景我的看法,但它被拉伸,以填补所有的观点。

任何解决方案(包括一些方法将一个视图下我目前的一个,使后者透明)将AP preciated。

谢谢!

目前的code:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
  机器人:背景=@可绘制/图标
  机器人:ID =@ + ID / layout_main
 >
 

解决方案

您有两个选择:

  1. 使图像更大。具体而言,添加一个黑线四周的边缘,然后使用工具/ draw9patch工具,以使该黑线'拉伸'的一部分。当图像被放大的背景下,原来的图标将不会被拉长。

  2. 放弃对安卓的背景,并使用顶级RelativeLayout的代替。这应该工作

 < RelativeLayout的
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    < ImageSwitcher
        机器人:ID =@ + ID / ImageSwitcher01
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_centerInParent =真
        机器人:layout_height =WRAP_CONTENT
        机器人:背景=@可绘制/图标>
    < / ImageSwitcher>
    <的LinearLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真正的>
        <的TextView
            机器人:文本=你的老顶级这里
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =WRAP_CONTENT>
        < / TextView的>
    < / LinearLayout中>
< / RelativeLayout的>
 

希望这有助于

I'd like to use a small (icon size) image as the background for my view, but it gets stretched to fill all the view.

Any solution (including some way to place a view under my current one and make the latter transparent) will be appreciated.

Thanks !

current code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  android:background="@drawable/icon"
  android:id="@+id/layout_main"
 >

解决方案

You have two choices:

  1. Make the image bigger. Specifically, add a single black line all around its edges, then use the tools/draw9patch tool to make that black line the 'stretchable' part. When the image is scaled up to the background, your original icon will not be stretched.

  2. Give up on the android:background and use a top level RelativeLayout instead. This should work

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageSwitcher
        android:id="@+id/ImageSwitcher01"
        android:layout_width="wrap_content"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content"
        android:background="@drawable/icon">
    </ImageSwitcher>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">
        <TextView
            android:text="Your old top level here"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
        </TextView>
    </LinearLayout>
</RelativeLayout>

Hope this helps

这篇关于Android的:如何在一个视图中居中背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆