Set child's style from the Viewgroup in Android?

帅比萌擦擦* 提交于 2019-12-11 01:18:21

问题


Say I want to set all the EditText fields in my Linear Layout to be single-line, how should I achieve that?

Detailed Code:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        style="@style/LoginTextFieldTheme"
        >
        <EditText 
            android:id="@+id/textPassword"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:inputType="textPassword"
            android:hint="password"
            />
        <EditText 
            android:id="@+id/textPasswordConfirm"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:inputType="textPassword"
            android:hint="password confirm"
            />
    </LinearLayout>

I want to specify something in the LoginTextFieldTheme, so that it can make my EditText to be single line.

I searched online, "style" doesn't applied to the child elements of the ViewGroup, and "theme" can be only applied to an activity.

There's something like applying "buttonStyle" to the style which is applied to the ViewGroup, but I cannot find the exact/complete documentation about what to do with the TextEdit.

Thanks a lot.


回答1:


You have few options to achieve this:

1. style.xml

You can apply style to EditText widget.

  • In style.xml in the values folder add this style:

    <style name="My.EditText.Style" parent="@android:style/Widget.EditText">
        <item name="android:singleLine">true</item>
    </style>
    
  • And for each EditText in your layout add: style="@style/My.EditText.Style"
    like:

    <EditText 
        android:id="@+id/textPassword"
        style="@style/My.EditText.Style"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:inputType="textPassword"
        android:hint="password"
    /> 
    

2. Extend EditText

You can extend and create your own custom EditText. Then, you can adjust your widget with singleLine to be true and then use this custom View in your layout.

3. Iterate over ViewGroup

You can iterate by using next ViewIterator over LinearLayout and check by instanceof for EditText and then change any property you need.

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
    }
}



回答2:


to set an edittext to one line you should use in the xml:

android:singleLine="true"

or

 android:maxLines="1"


来源:https://stackoverflow.com/questions/18747050/set-childs-style-from-the-viewgroup-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!