android - defStyleRes 在自定义 View (RelativeLayout) 中无效
问题描述
以下构造函数调用尝试RelativeLayout2
使用defStyleRes
参数设置我的自定义样式,但没有效果。我将此示例项目移植到 AndroidStudio,它工作正常。
public RelativeLayout2(Context context, IAttributeSet attributeSet)
: base(context, attributeSet, 0, Resource.Style.RelativeLayout2)
{
}
样式.xml
<resources>
<style name="RelativeLayout2">
<item name="android:layout_width">40dp</item>
<item name="android:layout_height">300dp</item>
<item name="android:background">#114499</item>
<item name="android:padding">50dp</item>
</style>
</resources>
activity_main.axml
<?xml version="1.0" encoding="utf-8"?>
<InflationWithStyle.RelativeLayout2
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="needs some style."/>
</InflationWithStyle.RelativeLayout2>
MainActivity.cs
namespace InflationWithStyle
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
}
}
public class RelativeLayout2 : RelativeLayout
{
public RelativeLayout2(Context context, IAttributeSet attributeSet)
: base(context, attributeSet, 0, Resource.Style.RelativeLayout2)
{
}
}
}
更新(2018.09.12)
我也尝试了样式化TextView
,并将其样式的父级设置为,android:Widget.TextView
但它也没有效果。
样式.xml
<style name="TextView2" parent="android:Widget.TextView">
<item name="android:background">#204090</item>
</style>
文本视图2
public class TextView2 : TextView
{
public TextView2(Context context, IAttributeSet attributeSet)
: base(context, attributeSet, 0, Resource.Style.TextView2)
{
}
}
activity_main.axml
<?xml version="1.0" encoding="utf-8"?>
<InflationWithStyle.RelativeLayout2
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appNS="http://schemas.android.com/apk/res/InflationWithStyle.InflationWithStyle"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--style="@style/RelativeLayout2"-->
<!--appNS:style1="@style/RelativeLayout2"-->
<InflationWithStyle.TextView2
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdfasdfasdfasfasfasdfasfdasdfasfasdf"
/>
<!--style="@style/TextView2"-->
</InflationWithStyle.RelativeLayout2>
解决方案
以下构造函数调用尝试使用 defStyleRes 参数设置我的自定义 RelativeLayout2 的样式,但没有效果。
Styles and Themes文档和Style 资源文档都没有暗示该//style/@name
值可以是View
名称并且具有“神奇链接”的事物。相反,它们都使用style
视图上的属性,例如
<?xml version="1.0" encoding="utf-8"?>
<CustomeView.RelativeLayout2
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/RelativeLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="An utter lack of style."/>
</CustomeView.RelativeLayout2>
我已经用这种方法测试了你的代码,它工作正常。
推荐阅读
- node.js - 将对象附加到节点中没有 fs.readFile 的 json 文件项,js
- postgresql - 转储和恢复 postgres 角色成员资格?
- excel - 在excel中将英文字符替换为马拉雅拉姆语字符
- c# - Web API 控制器失败,“ObjectContent`1”类型无法序列化响应正文
- date - Power Query 筛选器日期从今天减去 ? 到今天的几天
- python - 没有名为“惊喜”的模块
- r - 总结R中数据框中的重复项
- react-native - 世博会不显示日志
- javascript - 检查字段有效性和提交前填写的字段?
- python - for 循环获取 pandas 中的数据框