defStyleRes 在自定义 View (RelativeLayout) 中没有效果 [英] defStyleRes has no effect in custom View (RelativeLayout)

查看:12
本文介绍了defStyleRes 在自定义 View (RelativeLayout) 中没有效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下构造函数调用尝试使用 defStyleRes 参数为我的自定义 RelativeLayout2 设置样式,但没有效果.我将这个示例项目移植到 AndroidStudio,它运行正常.

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)
        {
        }
    }
}

<小时>

更新(2018.09.12)


Update (2018.09.12)

我也尝试为 TextView 设置样式,并将其样式的父级设置为 android:Widget.TextView 但它也没有效果.

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>

推荐答案

以下构造函数调用尝试使用 defStyleRes 参数设置我的自定义 RelativeLayout2 的样式,但没有效果.

The following constructor call attempts to style my custom RelativeLayout2 using the defStyleRes parameter but it has no effect.

样式和主题文档和样式资源 文档暗示 //style/@name 值可以是 View 名称,并且可以神奇地链接"事物.相反,它们都在视图上使用 style 属性,例如

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.

这篇关于defStyleRes 在自定义 View (RelativeLayout) 中没有效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆