android:background 能否写在style.xml里?

遊戲王千金 2011-06-27 11:52:21
现在遇到一个问题,就是常常使用EditText时候,运用到background图片,若每次都写,比较烦,想使用样式
<EditText
android:id="@+id/editText2"
android:paddingLeft="22.0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="40.0dip"
android:maxLength="12"
android:layout_alignParentLeft="true"
android:inputType="textPassword"
android:hint="密码"
style="@style/EditStyle"
android:background="@drawable/zs_edit_text" >
</EditText>

红色标注的内容,注zs_edit_text是使用selector作的响应默认焦点
代码如下

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<!-- 没有焦点时的背景图片 -->
<item
android:state_focused="true"
android:drawable="@drawable/login_edit_sel"
>
</item>
<!-- 默认时的背景图片-->
<item
android:drawable="@drawable/login_edit"
>
</item>
</selector>

记得若是开发web,在style里面就可以有background,但在android里面就报错.我android方面还是新手,
能否通过其它方式实现该效果,达到开发快捷.

<style name="EditStyle">
<item name="android:textSize">18sp</item>
<item name="android:fromAlpha">0.0</item>
<item name="android:toAlpha">0.0</item>
<!--<item name="android:background">@drawable/zs_edit_text</item> 报错-->
</style>
...全文
891 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tree_2002 2013-05-15
  • 打赏
  • 举报
回复
不清楚是咋解决的呢,没看出什么问题...
mahaiyun18 2011-06-28
  • 打赏
  • 举报
回复
·····接了
遊戲王千金 2011-06-28
  • 打赏
  • 举报
回复
找到正确方法.欢迎接分.
方法如下.
<item name="#1">#2</item>
1.item的name属性#1可以为所有系统所带组件的属性,#2为此属性的值如android:layout_height android:layout_widthandroid:background都可写成如下形式
<item name="android:background">#000000</item>
2.除此之外可以是任意你自己定义的属性 如: <item name="myKey">myValue</item>不同之处在于<item name="android:background">#000000</item>
由于是系统自带属性,所以可以直接在其他view的属性中引用此style.
自定义的属性要经过两个步骤才可以使用
1.values目录下创建一个attrs.xml文件 以如下方式声明 <attr name="myname" format="String" />
2.在一个style的item中以如下方式引用 <item name="myname">"我的名字"</item>
3.经过上两步就可以在自己的View中使MyView(Context context, AttributeSet attrs,int myStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes( attrs, R.styleable.TestView, myStyle, 0);
}
傲慢的上校 2011-06-28
  • 打赏
  • 举报
回复
学习了。。。
soclays 2011-06-28
  • 打赏
  • 举报
回复
学习了
遊戲王千金 2011-06-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pby_bob 的回复:]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="notitle_fullScreen">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
</r……
[/Quote]
谢,可这不是要想要的。能否提供一下android<item name="????">支持多少标签.
遊戲王千金 2011-06-27
  • 打赏
  • 举报
回复
ding
pby_bob 2011-06-27
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="notitle_fullScreen">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>

这个是我的style,你加上<resources>标签试下。应该会没问题的,希望能帮到楼主

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧