首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从ViewGroup继承android:tint属性

从ViewGroup继承android:tint属性
EN

Stack Overflow用户
提问于 2018-02-28 16:52:33
回答 1查看 407关注 0票数 1

如果为一个android:tint ViewGroup**,设置了属性,那么它应该应用于所有的后代** View**'s,,还是需要单独应用于每个子代?**

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

我想在android:tint中设置一次ViewGroup,这样当它被禁用时,它的所有成员都会被禁用并相应地更改它们的色调(而且也不必覆盖setEnabled)。

resources/layout/buttonbar_labeledbutton_addnew.axml

代码语言:javascript
复制
<?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

代码语言:javascript
复制
  <!--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

代码语言:javascript
复制
[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;
        }
    }    
}

更新

Attributes 每个视图和ViewGroup对象都支持它们自己的各种XML属性。有些属性是特定于视图对象的(例如,TextView支持textSize属性),但是这些属性也是由任何可能扩展该类的视图对象继承的。有些对象对所有View对象都是通用的,因为它们是从根视图类继承的(比如id属性)。另外,其他属性被认为是“布局参数”,这些属性描述视图对象的某些布局方向,由该对象的父ViewGroup对象定义。

android:tint是特定于ImageView的,并被忽略。我没有检查ButtonBar$LabeledButton的通胀构造函数中的属性集,以确定它是否至少可以使用。声明一个自定义属性可以解决这个问题,但是在它们现在需要的自定义类的通货膨胀构造器中,它的分配给ImageViewTextView会变得模糊(我更倾向于尽可能利用这个框架,以尽量减少它引入的维护和潜在故障点的任何额外的一次性代码b/c )。

EN

回答 1

Stack Overflow用户

发布于 2018-03-02 18:14:41

ButtonBar

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
  <!--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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49034712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档