主题和样式属性用于背景图片

8
我阅读了文档,它说:“样式可以指定诸如高度、填充、字体颜色、字体大小、背景颜色等属性。”现在我的问题是“更多”是什么意思。这是否意味着我可以为android:src定义不同的属性?就像这样:
<item name="android:src">@drawable/attachment1</item>

我尝试使用背景属性,它运行良好。

<item name="android:background">#55FFFFFF</item>

但是没有 src 属性 :-(。

我做错了什么?


你找到了什么吗? - Lukap
4个回答

6

尝试:

<item name="android:background">@drawable/attachment1</item>

它不会拉伸背景图片 - 只会缩放它。 - Dmitry

1
尝试使用这个:

<item name="android:windowBackground">@drawable/imageone</item>

0
在我的测试中并根据这篇文章 (如何在运行时更改TextView的样式),在样式中设置背景图像不起作用。
如果您正在以编程方式设置样式,请尝试像以下代码一样:
countField.setTextAppearance(getContext(),R.style.theme_id);
countField.setBackgroundResource(R.drawable.attachment1);

如果您在布局中设置样式,可以尝试类似以下的方式:

<TextView
  android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello world"
    android:layout_gravity="center"
    android:textAppearance="@style/MVTheme.TextAppearance.CalendarMissedCount"
    android:background="@drawable/attachment1 />

-1

你不能直接使用drawable。在res/drawable中创建一个xml文件。在style.xml中引用它。
res/drawable/attach.xml

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

res/values/styles.xml

<item name="android:src">@drawable/attach</item>

我还没有构建^^,但这是一般的想法。


在项目中,每张图片都有单独的样式文件并不是一个好的做法。 - Dmitry

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接