代码之家  ›  专栏  ›  技术社区  ›  samus

使用自定义样式参照设置视图样式

  •  0
  • samus  · 技术社区  · 6 年前

    我在试着设计一个 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)

    尽管如此,有没有一个更合适的,框架支持的方法来达到这个目的,因为这似乎是一种黑客?

    属性.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
      <declare-styleable name="RelativeLayout2">
        <attr name="style1" format="reference"></attr>
      </declare-styleable>
    
    </resources>
    

    样式.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>
    

    活动_主.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>
    

    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;
            }
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   York Shen    6 年前

    @style/RelativeLayout2中的任何样式都不会生效

    defStyleRes has no effect in custom View (RelativeLayout) .

    你可以试着用 style="@style/RelativeLayout2" ,对我来说效果不错。