Android:如何将样式应用于ViewGroup的所有子项

时间:2014-08-25 07:53:50

标签: android android-styles

我有一个我已应用于查看组的样式,但我想将该样式应用于viewgroup的所有子项。怎么办呢?

3 个答案:

答案 0 :(得分:2)

除此之外无法做到这一点:
您可以使用LinearLayout上的下一个ViewIterator进行迭代,并通过instanceof检查EditText,然后更改您需要的任何属性。

    public class ViewIterator implements java.util.Iterator<View>
{
    private java.util.Stack<View> mNodes;

    public ViewIterator(View view)
    {
        mNodes = new java.util.Stack<View>();
        if (view != null)
        {
            mNodes.push(view);
        }
    }

    public boolean hasNext()
    {
        return !mNodes.empty();
    }

    public View next()
    {
        View view = null;

        // if remaining nodes
        if (hasNext())
        {
            // return the last element
            view = mNodes.pop();
            assert (view != null);
            // if it is a container, add its children to the stack
            if (view instanceof ViewGroup || view instanceof AdapterView)
            {
                ViewGroup viewGroup = (ViewGroup)view;
                for (int i = 0; i < viewGroup.getChildCount(); i++)
                {
                    assert (viewGroup.getChildAt(i) != null);
                    mNodes.push(viewGroup.getChildAt(i));
                }
            }
        }

        // return result
        return view;
    }

    public void remove()
    {
        // not supported
    }
}

取自here

答案 1 :(得分:1)

如果在xml中将样式应用为主题,则可以将样式应用于另一个视图的所有视图子元素。

android:theme="@style/AppTheme.Inverted"

答案 2 :(得分:0)

我知道我迟到了,但希望这对未来的观众有所帮助! 我将详细说明如何为父视图及其子视图创建和应用主题。例如,如何更改 CardView 及其子视图的背景和文本颜色:

1.在 colors.xml 中创建一组颜色(这是可选的,因为您可以简单地在 colors.xml 项的值中包含颜色十六进制)

<resources>
    <!-- You can name the colors whatever you want, however, I like to follow 
Material naming conventions -->
    <color name="colorPrimary">#00796b</color>
    <color name="colorPrimaryLight">#48a999</color>
</resources>

2.在 styles.xml

中创建主题
<!-- You can name the style whatever you want, and extend whatever parent theme you want -->
<style name="CardView.Light.Details" parent="Theme.MaterialComponents.DayNight">

    <!-- These colors are referencing the colors defined above in colors.xml -->
    <item name="android:backgroundTint">@color/colorPrimaryLight</item>
    <item name="android:textColor">@color/colorOnPrimary</item>
</style>

3.插入样式名称作为视图的 android:theme 属性值

<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    app:cardCornerRadius="5dp"
    android:theme="@style/CardView.Light.Details"> <!-- Notice the theme here! -->

    ....

    <!-- No need to apply any theme or color attributes on child views
         (unless you would like to override the parent theme) -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/stock_text"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textSize="14sp" />

    ....
</com.google.material.card.MaterialCardView>