背景可绘制定义视图大小视图、定义、大小、背景

2023-09-06 02:18:29 作者:七夕.单身去泡妞

为了清楚起见,这个问题已被显著编辑的

让我们说我有100x100px的巴纽资源:

Let's say I have a .png resource of 100x100px:

我想使用图片作为背景为我的的TextView s,它的大小是由它的内容,这是可变的在运行时确定。特别是,我想他的形象来平铺在的TextView 尺寸比100x100px大,我希望它被裁剪当的TextView 比100x100px小。

I want to use that image as a background for my TextViews, which size is determined by its content, which is variable at runtime. Especially, I want that image to tile when the TextView size is larger than 100x100px, and I want it to be cropped when the TextView is smaller than 100x100px.

后者似乎是一个困难的一年。

The latter seems to be a difficult one.

我有一个绘制资源 bg_tile.xml

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/bg"
    android:tileMode="repeat" />

我的布局文件:

My layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/bg_tile"
        android:text="Short text"
        android:textSize="20sp" />

    <TextView android:layout_margin="8dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_tile"
        android:text="Long text long text long text long text long text long text long text long text long textlong text long text long text long text long text long text long text long text long textlong text long text long text long text long text long text long text long text long text "
        android:textSize="20sp" />

</LinearLayout>

这是怎么回事

下面是这个code结果的截图,和我会想到一/我想要的东西:

What's happening

Here is a screenshot of the result of this code, and one of what I would expect / what I want:

正如你所看到的,平铺的部分作品,底部片被切断。然而,当的TextView 比大背景下,的TextView 取背景的尺寸更小。

As you can see, the tiling part works, and the bottom tile is being cut off. However, when the TextView is smaller than the background, the TextView takes the size of the background.

Android的:重力=clip_vertical | clip_horizo​​ntal - 这是不行的,即使没有TILEMODE 使用 match_parent 作为布局PARAMS - 不是一个选项 设置一个固定的高度 - 有没有办法告诉多久的含量的TextView 会,并与它的大小 android:gravity="clip_vertical|clip_horizontal" - This doesn't work, even without tilemode. Use match_parent as layout params - Not an option. Setting a fixed height - There is no way of telling how long the content of the TextView will be, and with that its size.

如何才能实现我的目标是什么?

How can I achieve my goal?

推荐答案

我还没试过此我自己,但你可以尝试这样的想法/破解/解决方法:

I have not tried this myself, but you could try this idea/hack/workaround:

的视图(TextView中)的最小高度/宽度由它的背景可提拉法(除其他事项外)。它采用了背景绘制对象的的getMinimumHeight()和getMinimumWidth()来确定视图的最小高度和宽度。

The minimum height/width of a View (TextView) is determined by its background drawable (amongst other things). It uses the background Drawable's getMinimumHeight() and getMinimumWidth() to determine the View's minimum height and width.

它看起来像你想查看的最小高度和宽度为0(不包括填充)和视图的宽度/高度应只基于浏览的内容(在你的情况,TextView的文本)。

It looks like you want the View's minimum height and width to be 0 (excluding padding) and the View's width/height should be based only on the View's content (in your case, the TextView's text).

尝试在的的的onCreate 的你的活动或的 onCreateView 的你的片段,你在哪里得到一抱你想'修复'的TextView中的。让我们把这个TextView的TV:

Try this in the onCreate of your Activity or the onCreateView of your Fragment, where you get a hold of the TextView you are trying to 'fix'. Let's call this TextView 'tv':

TextView tv; 
...
...
tv = (TextView)...findViewById(...);
....

// Swap the 'standard' bitmap background with one that has no minimum width/height.
BitmapDrawable background = (BitmapDrawable)tv.getBackground(); // assuming you have bg_tile as background.
BitmapDrawable newBackground = new BitmapDrawable(background.getBitmap) {
    @Override
    public int getMinimumWidth() {
        return 0;
    }

    @Override
    public int getMinimumHeight() {
        return 0;
    }
};
newBackground.setTileModeXY(background.getTileModeX(), background.getTileModeY());
tv.setBackgroundDrawable(newBackground);
...
...