【问题标题】:Styling a view with a custom style reference使用自定义样式引用样式化视图
【发布时间】:2018-09-06 19:42:23
【问题描述】:

我正在尝试使用自定义属性设置 RelativeLayout 的样式

appNS:style1="@style/RelativeLayout2"

这个属性的值是在构造的时候用静态方法读取的

GetStyle(context, attributeSet)

并传递给适当的基础构造函数

public RelativeLayout2(Context context, IAttributeSet attributeSet) 
    : base(context, attributeSet, 0, GetStyle(context, attributeSet))
{
}

@style/RelativeLayout2 中的样式均未生效,可能与单独的问题有关 defStyleRes has no effect in custom View (RelativeLayout)

尽管如此,是否有更合适的、框架支持的手段来达到这个目的,因为这种方法看起来像是一种黑客行为?

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;
        }
    }
}

【问题讨论】:

    标签: android xamarin.android


    【解决方案1】:

    @style/RelativeLayout2 中的样式都没有生效

    请参考我的回答defStyleRes has no effect in custom View (RelativeLayout)

    您可以尝试使用style="@style/RelativeLayout2",它对我来说效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-08
      • 1970-01-01
      • 2017-09-14
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多