问题
The following constructor call attempts to style my custom RelativeLayout2 using the defStyleRes parameter but it has no effect. I ported this sample project to AndroidStudio and it worked ok.
public RelativeLayout2(Context context, IAttributeSet attributeSet)
: base(context, attributeSet, 0, Resource.Style.RelativeLayout2)
{
}
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: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)
{
}
}
}
Update (2018.09.12)
I tried styling a TextView as well, and set it's style's parent to android:Widget.TextView but it also had no effect.
styles.xml
<style name="TextView2" parent="android:Widget.TextView">
<item name="android:background">#204090</item>
</style>
TextView2
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>
回答1:
The following constructor call attempts to style my custom RelativeLayout2 using the defStyleRes parameter but it has no effect.
Neither the Styles and Themes documentation nor the Style resource documentation implies that the //style/@name value can be a View name and have things "magically linked". Instead, they both use a style attribute on the View, e.g.
<?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>
I have tested your code with this method, it works fine.
来源:https://stackoverflow.com/questions/52210674/defstyleres-has-no-effect-in-custom-view-relativelayout