从ViewGroup继承android:tint属性 [英] Inheriting the android:tint attribute from a ViewGroup

查看:53
本文介绍了从ViewGroup继承android:tint属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果为ViewGroup设置了android:tint属性,则该属性应应用于所有后代View,还是需要单独应用于每个后代?

If the android:tint attribute is set for a ViewGroup, should it apply to all descendant View's, or does it need to be applied to each individually?

下面的LinearLayout(ButtonBar$LabeledButton)包含一个ImageViewTextView,它们分别指定自己的颜色状态列表(CSL).

The LinearLayout below (ButtonBar$LabeledButton) contains an ImageView and TextView that each specify their own color state list (CSL).

我想在ViewGroup中设置一次android:tint,因此当它被禁用时,其所有成员都将被禁用并相应地更改其色度(并且也不必覆盖setEnabled).

I'd like to set android:tint once in the ViewGroup so when it is disabled, all its members become disabled and change their tint accordingly (and also without having to override the setEnabled).

资源/布局/buttonbar_labeledbutton_addnew.axml

<?xml version="1.0" encoding="utf-8"?>
<!--LinearLayout-->
<view class="ButtonBar$LabeledButton"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/ButtonBar_LabeledButton_AddNew"
  style="@style/ButtonBar_LabeledButton">

  <ImageView
    android:id="@+id/ButtonBar_LabeledButton_Image"
    style="@style/ButtonBar_LabeledButton_Image"
    android:src="@drawable/v__ic_add_circle_outline_black_24dp"/>

  <TextView
    android:id="@+id/ButtonBar_LabeledButton_Label"
    style="@style/ButtonBar_LabeledButton_Label"
    android:text="Add New"/>

</view>
<!--/LinearLayout-->

resources/values/styles.xml

  <!--LinearLayout-->
  <style name="ButtonBar_LabeledButton">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:orientation">horizontal</item>
    <??-item name="android:tint">@color/buttonbar_csl</item-??>
  </style>

  <!--ImageView-->
  <style name="ButtonBar_LabeledButton_Image">
    <item name="android:layout_width">40dp</item>
    <item name="android:layout_height">40dp</item>
    <item name="android:tint">@color/buttonbar_csl</item>
  </style>

   <!--TextView-->
  <style name="ButtonBar_LabeledButton_Label">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">@color/buttonbar_csl</item>
    <!--item name="android:tint">@color/buttonbar_csl</item-->
  </style>

ButtonBar.LabeledButton

[Register("ButtonBar$LabeledButton")]
public class LabeledButton : LinearLayout
{
    public LabeledButton(Context context, IAttributeSet attributes) : base(context, attributes) { }

    public override bool Enabled
    {
        set
        {
            var image = FindViewById<ImageView>(Resource.Id.ButtonBar_LabeledButton_Image);
            if( image != null )
                image.Enabled = value;

            var label = FindViewById<TextView>(Resource.Id.ButtonBar_LabeledButton_Label);
            if( label != null )
                label.Enabled = value;

            base.Enabled = value;
        }
    }    
}


更新

属性

每个View和ViewGroup对象都支持自己的各种XML 属性.一些属性特定于View对象(对于 例如,TextView支持textSize属性),但是这些 属性也可以被可能扩展此属性的所有View对象继承 班级.有些是所有View对象共有的,因为它们是继承的 从根View类(如id属性)开始.和别的 属性被视为布局参数",即属性 描述View对象的某些布局方向,例如 由该对象的父ViewGroup对象定义.

Every View and ViewGroup object supports their own variety of XML attributes. Some attributes are specific to a View object (for example, TextView supports the textSize attribute), but these attributes are also inherited by any View objects that may extend this class. Some are common to all View objects, because they are inherited from the root View class (like the id attribute). And, other attributes are considered "layout parameters," which are attributes that describe certain layout orientations of the View object, as defined by that object's parent ViewGroup object.

android:tint特定于ImageView,并且被忽略.我没有检查ButtonBar$LabeledButton的通胀构造函数中设置的属性,以查看它是否至少可以使用.声明一个自定义属性可以解决此问题,但随后将它们分配给现在要求的自定义类的通货膨胀构造函数中的ImageViewTextView(我更喜欢尽可能利用该框架,以最大程度地减少任何额外的负担,它引入的维护和潜在故障点的off-off代码b/c.

android:tint is specific to ImageView, and is ignored. I didn't inspect the attribute set in ButtonBar$LabeledButton's inflation constructor to see if it is at lest present to work with. Declaring a custom attribute would solve this, but then it's assignment to the ImageView and TextView in their now required custom classes' inflation constructors would be obscured (I prefer to leverage the framework as much as possible to minimize any extra, one-off code b/c of the maintenance and potential point-of-failure it introduces).

推荐答案

ButtonBar

public class ButtonBar : LinearLayout
{
    public ButtonBar(Context context, IAttributeSet attributes) : base(context, attributes) { }

    public override bool Enabled
    {
        set
        {
            SetChilderenEnabled(value);

            base.Enabled = value;
        }
    }

    private void SetChilderenEnabled(bool value)
    {
        for (int i = 0; i < ChildCount; i++)
        {
            GetChildAt(i).Enabled = value;
        }
    }

    [Register("us.sam.views.ButtonBar$LabeledButton")]
    public class LabeledButton : LinearLayout
    {
        private ImageView _buttonIV;
        private TextView _labelTV;
        private int _buttonIV_src;
        private string _labelTV_text;

        public override bool Enabled
        {
            set
            {
                if (_buttonIV != null)
                    _buttonIV.Enabled = value;

                if (_labelTV != null)
                    _labelTV.Enabled = value;

                base.Enabled = value;
            }
        }

        public LabeledButton(Context context, IAttributeSet attributes) : base(context, attributes)
        {
            ReadAttributes(context, attributes);

            LayoutInflater inflater = LayoutInflater.From(context);

            _labelTV = (TextView)inflater.Inflate(Resource.Layout.ButtonBar_LabeledButton_LabelTextView, this, false);
            _buttonIV = (ImageView)inflater.Inflate(Resource.Layout.ButtonBar_LabeledButton_ButtonImageView, this, false);

            _labelTV.Text = _labelTV_text;
            _buttonIV.SetImageResource(_buttonIV_src);

            AddViewInLayout(_buttonIV, ChildCount, _buttonIV.LayoutParameters);
            AddViewInLayout(_labelTV, ChildCount, _labelTV.LayoutParameters);
        }

        private void ReadAttributes(Context context, IAttributeSet attributes)
        {
            Android.Content.Res.TypedArray typedArray = context.ObtainStyledAttributes(attributes, Resource.Styleable.LabeledButton);

            _buttonIV_src = typedArray.GetResourceId(Resource.Styleable.LabeledButton_button_imageview_src, 0);
            _labelTV_text = typedArray.GetString(Resource.Styleable.LabeledButton_label_textview_text);

            typedArray.Recycle();
        }
    }
}

recordexpandablelistview_groupbuttonbar_trecords.axml

<?xml version="1.0" encoding="utf-8"?>
<!--LinearLayout-->
<view class="us.sam.RecordsView$RecordExpandableListView$GroupButtonBar"
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:id="@+id/RecordExpandableListView_GroupButtonBar_TRecords"
  style="@style/RecordExpandableListView_GroupButtonBar">

  <!--LinearLayout-->
  <us.sam.Views.ButtonBar
    android:id="@+id/ButtonBar"
    style="@style/ButtonBar">

    <include layout="@layout/ButtonBar_LabeledButton_AddNew"/>

  </us.sam.Views.ButtonBar>

  <ImageView style="@style/ListItem_Divider_Horizontal"/>

</view>

ButtonBar_LabeledButton_AddNew.axml

<?xml version="1.0" encoding="utf-8"?>
<!--LinearLayout-->
<view class="us.sam.views.ButtonBar$LabeledButton"
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:id="@+id/ButtonBar_LabeledButton_AddNew"
  style="@style/ButtonBar_LabeledButton"
  button_imageview_src="@drawable/v__ic_add_circle_outline_black_24dp"
  label_textview_text="@string/ButtonBar_LabeledButton_LabelTextView_Text_AddNew">
</view>

ButtonBar_LabeledButton_ButtonImageView.axml

<?xml version="1.0" encoding="utf-8"?>
<!--<view class="us.sam.views.ButtonBar$LabeledButton$Button"-->
<ImageView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/ButtonBar_LabeledButton_ButtonImageView"
  style="@style/ButtonBar_LabeledButton_ButtonImageView"/>
<!--android:src="@drawable/v__ic_add_circle_outline_black_24dp"-->

ButtonBar_LabeledButton_LabelTextView.axml

<?xml version="1.0" encoding="utf-8"?>
<!--view class="us.sam.views.ButtonBar$LabeledButton$Label"-->
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/ButtonBar_LabeledButton_LabelTextView"
  style="@style/ButtonBar_LabeledButton_LabelTextView"/>
<!--android:text="Add New"-->

styles.xml

  <!--LinearLayout-->
  <style name="RecordExpandableListView_GroupButtonBar">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:orientation">vertical</item>
    <!--use isChildSelectable() override in BaseExpandableListAdapter instead-->
    <!--item name="android:clickable">true</item-->
  </style>

  <!--LinearLayout-->
  <style name="ButtonBar">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:orientation">horizontal</item>
  </style>

  <!--LinearLayout-->
  <style name="ButtonBar_LabeledButton">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginLeft">10dp</item>
    <item name="android:orientation">horizontal</item>    
  </style>

    <!--ImageView-->
  <style name="ButtonBar_LabeledButton_ButtonImageView">
    <item name="android:layout_width">40dp</item>
    <item name="android:layout_height">40dp</item>    
    <item name="android:tint">@color/button_bar_csl</item>    
  </style>

  <!--TextView-->
  <style name="ButtonBar_LabeledButton_LabelTextView">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginLeft">10dp</item>
    <item name="android:layout_gravity">center_vertical</item>
    <item name="android:gravity">center_vertical</item>
    <item name="android:textSize">14sp</item>    
    <item name="android:textColor">@color/button_bar_csl</item>    
  </style>

  <style name="ListItem_Divider_Horizontal">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">1px</item>
    <item name="android:background">@android:color/black</item>
  </style>

这篇关于从ViewGroup继承android:tint属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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