从在的ViewGroup Android的子集的风格? [英] Set child's style from the Viewgroup in Android?
问题描述
说我要设置我的线性布局中的所有的EditText字段是单行,我应该怎么实现呢?
Say I want to set all the EditText fields in my Linear Layout to be single-line, how should I achieve that?
详细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>
我要指定在LoginTextFieldTheme东西,所以它能够使我的EditText为单线。
I want to specify something in the LoginTextFieldTheme, so that it can make my EditText to be single line.
我在网上搜索,风格并不适用于的ViewGroup的子元素和主题,可以只适用于一个活动。
I searched online, "style" doesn't applied to the child elements of the ViewGroup, and "theme" can be only applied to an activity.
有类似应用按钮样式要应用到的ViewGroup的风格,但我找不到什么与做文字编辑的确切/完整的文档。
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.
非常感谢。
推荐答案
您有几个选项来实现这一点:
You have few options to achieve this:
您可以应用样式的EditText
部件。
-
在
style.xml
在值的文件夹添加这种风格:
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>
和每个的EditText
在布局中添加:风格=@风格/ My.EditText.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"
/>
您可以扩展和创建自己的定制的EditText。然后,您可以用 SINGLELINE
调整你的部件是真实的,然后使用你的布局此自定义视图。
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.
您可以通过使用迭代下一 ViewIterator
在的LinearLayout
和的instanceof 检查code>为
的EditText
,然后改变你需要的任何财产。
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
}
}
这篇关于从在的ViewGroup Android的子集的风格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!