问题
I'm trying to style a RelativeLayout with a custom attribute
appNS:style1="@style/RelativeLayout2"
The value of this attribute is read during construction with the static method
GetStyle(context, attributeSet)
and passed to the appropriate base constructor
public RelativeLayout2(Context context, IAttributeSet attributeSet)
: base(context, attributeSet, 0, GetStyle(context, attributeSet))
{
}
None of the styling in @style/RelativeLayout2 takes effect, and may be related to a separate issue defStyleRes has no effect in custom View (RelativeLayout)
Despite that, is there a more appropriate, framework supported mean to this end, since this sort of seems like a hack?
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RelativeLayout2">
<attr name="style1" format="reference"></attr>
</declare-styleable>
</resources>
styles.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: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"
appNS:style1="@style/RelativeLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Needs more style."/>
</InflationWithStyle.RelativeLayout2>
MainActivity.cs
using Android.App;
using Android.Content;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Util;
using Android.Widget;
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, GetStyle(context, attributeSet))
{
}
private static int GetStyle(Context context, IAttributeSet attributeSet)
{
return ReadStyleAttribute(context, attributeSet);
}
private static int ReadStyleAttribute(Context context, IAttributeSet attributes)
{
Android.Content.Res.TypedArray typedArray = context.ObtainStyledAttributes(attributes, Resource.Styleable.RelativeLayout2);
int styleAttr = typedArray.GetResourceId(Resource.Styleable.RelativeLayout2_style1, 0);
typedArray.Recycle();
return styleAttr;
}
}
}
回答1:
None of the styling in @style/RelativeLayout2 takes effect
Please refer to my answer defStyleRes has no effect in custom View (RelativeLayout).
You could try using style="@style/RelativeLayout2", it works fine on my side.
来源:https://stackoverflow.com/questions/52211138/styling-a-view-with-a-custom-style-reference