Android 背景绘图可定义视图大小

Android 背景绘图可定义视图大小,android,Android,为了清楚起见,对该问题进行了重大编辑 假设我有一个100x100px的.png资源: 我想将该图像用作我的TextViews的背景,其大小由其内容决定,在运行时是可变的。特别是,当TextView大小大于100x100px时,我希望该图像平铺,当TextView小于100x100px时,我希望对其进行裁剪 后者似乎是一个困难的问题 示例代码 我有一个可绘制的资源bg_tile.xml: <bitmap xmlns:android="http://schemas.android.com/a

为了清楚起见,对该问题进行了重大编辑

假设我有一个100x100px的.png资源:

我想将该图像用作我的
TextView
s的背景,其大小由其内容决定,在运行时是可变的。特别是,当
TextView
大小大于100x100px时,我希望该图像平铺,当
TextView
小于100x100px时,我希望对其进行裁剪

后者似乎是一个困难的问题

示例代码 我有一个可绘制的资源
bg_tile.xml

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

我的布局文件:

<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>

发生了什么事 以下是此代码结果的屏幕截图,以及我期望/想要的内容之一:

如您所见,瓷砖部分工作正常,底部瓷砖被切断。但是,当
TextView
小于背景时,
TextView
采用背景的大小

建议的解决方案
  • android:gravity=“clip_vertical | clip_horizontal”
    -即使没有tilemode,这也不起作用
  • 使用
    match_parent
    作为布局参数-不是选项
  • 设置固定高度-无法确定
    TextView
    内容的长度以及大小

我怎样才能实现我的目标?

我猜你的问题就在这里:

android:layout\u width=“包装内容” android:layout\u height=“包装内容”

它设置包裹内容的宽度,内容包括背景图像。我不确定视图的上下文是什么,但如果您只需从代码或xml文件设置高度,您的问题就应该消失了。如果背景仍然不符合您的喜好,您可能还需要添加android:scaleType=“centerCrop”或Houcine提到的centerInside

如果您告诉我们这些模糊视图的上下文,我们可能会为您提供更多信息。

您可以随时使用:

android:layout_width="match_parent"
android:layout_height="match_parent"
试试这个:

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/bg_light"
    android:tileMode="repeat"
    android:gravity="clip_vertical|clip_horizontal" />

如果您提前知道视图的大小,并且知道它会被剪裁,那么可以使用一个位图XML来表示tileMode,另一个位图XML来表示gravity,然后使用适当的位图XML。不幸的是,这并不理想。

我自己也没有尝试过,但你可以尝试一下这个想法/破解/解决方法:

视图(TextView)的最小高度/宽度由其可绘制的背景(以及其他内容)决定。它使用背景绘图表的getMinimumHeight()和getMinimumWidth()来确定视图的最小高度和宽度

看起来您希望视图的最小高度和宽度为0(不包括填充),并且视图的宽度/高度应仅基于视图的内容(在您的情况下,文本视图的文本)

在活动的onCreate或片段的onCreateView中尝试这一点,在这里您可以获得您试图“修复”的TextView。我们将此文本视图称为“电视”:

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);
...
...

这个方法适合我

  • 清除文本视图背景
  • (在postRunnable中)保存文本视图宽度和高度
  • (在postRunnable中)将新背景设置为TextView
  • (在postRunnable中)将宽度和高度设置为TextView
  • 示例

    public void setTextViewBg(最终文本视图文本视图,最终可绘制新图形){
    textView.setBackground(空);
    textView.post(新的Runnable(){
    @凌驾
    公开募捐{
    int width=textView.getWidth();
    int height=textView.getHeight();
    textView.setBackgroundDrawable(newBgDrawable);
    LinearLayout.LayoutParams LayoutParams=(LayoutParams)textView.getLayoutParams();
    layoutParams.width=宽度;
    layoutParams.height=高度;
    }
    });
    }
    

    希望它能帮助~

    修复您的
    视图的
    高度
    android:layout\u height=“75dp”
    ,并使用
    android:scaleType=“centerCrop”
    centerInside
    这是不可能的,因为视图可能是具有多行文本的
    TextView
    。添加您的代码并指定要设置背景的视图类型,这不重要,我使用的是不同类型的
    视图
    具有动态大小。感谢您的输入!虽然这确实会使我的
    文本视图的大小正确,但图像会缩小并拉伸到合适的大小。请尝试调用newBackground.setTileModex(background.getTileModeX(),background.getTimeModeY());在打电话给电视之前。挫折感…谢谢,这很有效!虽然它使用了不推荐使用的方法,但我会找到一种方法来解决这个问题。是的,“setBackgroundDrawable”是不推荐使用的,但是如果您的代码需要在API级别15或更低的级别上运行,您就不能使用“setBackground”方法……非常好、非常巧妙的解决方案。
    
    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);
    ...
    ...