使用自定义样式引用设置视图样式

问题描述 投票:0回答:1

我正在尝试使用自定义属性设计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个回答
0
投票

@ style / RelativeLayout2中的任何样式都不起作用

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

您可以尝试使用style="@style/RelativeLayout2",它在我身边工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.