首页 > 解决方案 > 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>

标签: androidxamarin.android

解决方案


以下构造函数调用尝试使用 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>

我已经用这种方法测试了你的代码,它工作正常。


推荐阅读