瓷砖的背景是推动它的视图尺寸 [英] Tiled background is pushing it's View size

查看:151
本文介绍了瓷砖的背景是推动它的视图尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用的查看背景铺位图。这查看,让我们说,有安卓layout_height =WRAP_CONTENT。的问题是,在背景中使用的位图的高度正在参与的视图的测量,增加查看高度。这可以注意到当查看的内容的大小是比用作瓦片背景的位图的高度小。

I have a tiled bitmap that I'm using as the View background. This View, let's say, has android:layout_height="wrap_content". The problem is that the height of the bitmap used in the background is participating in the measurement of the view, increasing the View height. This can be noticed when the size of the content of View is smaller than the height of the bitmap used as the tile background.

让我告诉你一个例​​子。瓷砖位图:

Let me show you an example. The tile bitmap:

位图绘制( tile_bg.xml ):

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/tile"
    android:tileMode="repeat"/>

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FFFFFF">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/tile_bg"
        android:text="@string/hello"
        android:textColor="#000000" />

</LinearLayout>

这怎么是这样的:

How it looks like:

的高度的TextView 最终被位图的高度。我所期待的是,位图被夹在查看的大小。

The height of the TextView ends up being the height of the bitmap. What I was expecting is that the bitmap gets clipped to the size of the View.

有什么办法来实现这一目标?

Is there any way to achieve this?

注:

  • 我不能使用9patch可绘制,因为背景需要重复在平铺的方式方法,拉伸不是一个选项。
  • 在我不能设置为查看固定高度,这取决于孩子(我在的ViewGroup
  • 在这种奇怪的行为发生,因为我的时候的的尺寸显示比位图的尺寸小前解释,否则位图重复的正确修剪(即,如果视图大小是1.5倍的位图的大小,你最终看到的位图的1.5倍)。
  • 该例涉及的高度,但使用的宽度是相同的。
  • I can't use 9patch drawables since the background needs to be repeated in a tile fashion way, stretching is not an option.
  • I can't set a fixed height for the View, it depends of the children (I'm using this in a ViewGroup)
  • This odd behavior happens as I explained before when the size of the View is smaller than the size of the bitmap, otherwise the bitmap is repeated an clipped correctly (ie, if the view size is 1.5x the size of the bitmap, you end up seeing 1.5 times the bitmap).
  • The example deals with the height, but is the same using the width.

推荐答案

您需要一个定制BitmapDrawable返回0从的getMinimumHeight()和getMinimumWidth()。这里有一个我命名BitmapDrawableNoMinimumSize由它来完成的工作:

You need a custom BitmapDrawable that returns 0 from getMinimumHeight() and getMinimumWidth(). Here's one I've named BitmapDrawableNoMinimumSize which does the job:

import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;

public class BitmapDrawableNoMinimumSize extends BitmapDrawable {

    public BitmapDrawableNoMinimumSize(Resources res, int resId) {
        super(res, ((BitmapDrawable)res.getDrawable(resId)).getBitmap());
    }

    @Override
    public int getMinimumHeight() {
        return 0;
    }
    @Override
    public int getMinimumWidth() {
         return 0;
    }
}

当然,你不能(据我所知)声明自定义可绘在XML,所以你必须初始化并设置TextView的背景正是如此:

Of course you can't (AFAIK) declare custom drawables in XML, so you have to instantiate and set the textview's background thusly:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    BitmapDrawable bmpd =new BitmapDrawableNoMinimumSize(getResources(), R.drawable.tile);
    bmpd.setTileModeX(TileMode.REPEAT);
    bmpd.setTileModeY(TileMode.REPEAT);
    findViewById(R.id.textView).setBackgroundDrawable(bmpd);
}

当然,你从布局XML去除背景属性:

And of course you remove the background attribute from the layout xml:

<TextView
    android:id="@+id/textView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Testing testing testing"
    android:textColor="#000000" />

我测试过这一点,似乎工作。

I've tested this and it seems to work.

这篇关于瓷砖的背景是推动它的视图尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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