android引用style中的样式,android - 如何从drawable引用样式属性?
时间: 2024-02-26 10:57:56 浏览: 187
要从drawable中引用样式属性,您可以使用以下步骤:
1. 在styles.xml文件中定义一个样式属性,例如:
```xml
<style name="MyImageStyle">
<item name="android:tint">@color/my_tint_color</item>
</style>
```
2. 在drawable文件中使用样式属性,例如:
```xml
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_image"
android:tint="?attr/my_tint_color" />
```
在此示例中,`?attr/my_tint_color`将引用样式属性中定义的`android:tint`属性。如果您希望为不同的drawable使用不同的样式属性,可以在每个drawable中使用不同的`?attr`引用。
注意,此方法仅适用于API级别为21及更高版本的Android设备。如果您需要支持旧版本的设备,请考虑使用不同的方法,如设置drawable的颜色过滤器。
相关问题
<resources> <style name=”AppBaseTheme” parent=”android:Theme.Light”></style> <style name=”AppTheme” parent=”AppBaseTheme”></style> <!--ListView自定义控件--> <style name=”customProgressBar” parent=”@android:style/Widget.ProgressBar.Small”> <item name=”android:indeterminateDravable”>@drawable/custom_progress_bar</item> <item name=”android:layout_vidth”>21dip</item> <item name=”android:layout_height”>21dip</item> <item name=”android:layout_gravity”>center</item> </style> </resources>这段代码是什么意思
这段代码是一个Android应用程序的样式文件,主要定义了三个样式:AppBaseTheme、AppTheme和customProgressBar。
AppBaseTheme是一个基础主题样式,继承了Android系统自带的Theme.Light主题。这个样式没有任何自定义的属性。
AppTheme是一个应用主题样式,继承了AppBaseTheme样式。这个样式也没有任何自定义的属性,只是继承了AppBaseTheme的属性。
customProgressBar是一个自定义进度条控件的样式。这个样式继承了Android系统自带的Widget.ProgressBar.Small样式,并设置了以下几个属性:
1. android:indeterminateDravable:设置进度条的背景图案,这里使用了一个自定义的进度条背景图案。
2. android:layout_vidth:设置进度条的宽度为21dip。
3. android:layout_height:设置进度条的高度为21dip。
4. android:layout_gravity:设置进度条在父布局中的位置为中心。
这样定义了customProgressBar样式后,我们就可以在布局文件中使用这个样式来自定义进度条控件的样式了。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="6dp"> <ImageView android:id="@+id/iv_head" android:layout_width="65dp" android:layout_height="65dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="5dp" android:background="@drawable/robot_head" android:focusable="false"/> <TextView android:id="@+id/tv_chat_content" style="@style/chat_content_style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_toEndOf="@+id/iv_head" android:background="@drawable/chat_left_selector""困难点
这段代码是一个聊天界面的布局文件,主要使用了RelativeLayout布局和ImageView、TextView控件。
其中,ImageView控件用于显示头像,通过设置android:background属性设置头像的图片资源,并通过android:layout_alignParentLeft、android:layout_alignParentTop、android:layout_marginTop等属性设置头像在布局中的位置和大小。
TextView控件用于显示聊天内容,通过设置android:background属性设置聊天内容的背景样式,android:layout_toEndOf属性将TextView控件放置在ImageView控件的右侧,并通过android:layout_marginLeft属性设置两者之间的间距。
该布局文件的困难点可能在于理解RelativeLayout布局的特点和控件的属性设置,例如android:layout_alignParentLeft、android:layout_alignParentTop、android:layout_toEndOf、android:background等属性的使用,以及控件的位置和大小的设置。此外,需要理解如何使用样式(style)来设置TextView控件的样式。
阅读全文
相关推荐
















