英文:
How to set EditText style from styles.xml in Android?
问题
我正在生成EditText
字段,并希望从styles.xml文件设置其样式。但是无法成功。我可以设置一些参数,但我更愿意使用styles.xml中的样式,这样以后如果需要更改会更容易。
styles.xml
<style name="input_fields_single_line">
<item name="android:padding">8dp</item>
<item name="android:background">@drawable/input_field_shape</item>
<item name="android:maxLines">1</item>
</style>
Java代码:
List<EditText> inputFieldList = new ArrayList<EditText>();
public void generateInputFields() {
EditText editTextFieldTitle = new EditText(this);
inputFieldList.add(editTextFieldTitle);
editTextFieldTitle.setHint(R.string.enter_field_title);
editTextFieldTitle.setMaxLines(1);
editTextFieldTitle.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
editTextFieldTitle.setFocusableInTouchMode(true);
editTextFieldTitle.requestFocus();
editTextFieldTitle.setStyle(R.style.input_fields_single_line); // 使用样式
myLayout.addView(editTextFieldTitle);
}
英文:
I'm generating EditText
fields and wish to set it's style from styles.xml file. But can't manage to do it. I can set some parameters, but I would rather use a style from styles.xml so it would be easier to change later if needed.
styles.xml
<style name="input_fields_single_line">
<item name="android:padding">8dp</item>
<item name="android:background">@drawable/input_field_shape</item>
<item name="android:maxLines">1</item>
</style>
Java code:
List<EditText> inputFieldList = new ArrayList<EditText>();
public void generateInputFields() {
EditText editTextFieldTitle = new EditText(this);
inputFieldList.add(editTextFieldTitle);
editTextFieldTitle.setHint(R.string.enter_field_title);
editTextFieldTitle.setMaxLines(1);
editTextFieldTitle.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
editTextFieldTitle.setFocusableInTouchMode(true);
editTextFieldTitle.requestFocus();
myLayout.addView(editTextFieldTitle);
}
答案1
得分: 1
你可以为 EditText 创建一个单独的样式,类似于你正在做的,但将其父级设置为 Widget.EditText,代码如下:
<style name="MyCustomeEditTextStyle" parent="@android:style/Widget.EditText">
...根据需要添加项目
<item name="android:padding">8dp</item>
<item name="android:background">@drawable/input_field_shape</item>
<item name="android:maxLines">1</item>
</style>
然后在 XML 中的每个编辑框中调用你的样式,如下所示:
<EditText
android:id="@+id/editTextId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/MyCustomeEditTextStyle" />
但是,如果你想要全局设置一次,并且由于你是以编程方式生成 EditText,类似于以下方式:
EditText editTextFieldTitle = new EditText(this);
那么你可以在应用程序样式中设置你的样式,如下所示:
<style name="AppTheme" parent="@android:style/YourParentTheme">
<item name="android:editTextStyle">@style/MyCustomeEditTextStyle</item>
</style>
英文:
You create a separate style for the EditText like what you are doing but parented with the Widget.EditText like the following:
<style name="MyCustomeEditTextStyle" parent="@android:style/Widget.EditText">
...add as many items as you need
<item name="android:padding">8dp</item>
<item name="android:background">@drawable/input_field_shape</item>
<item name="android:maxLines">1</item>
</style>
Then call your style inside each edit text in the xml like the following:
<EditText
android:id="@+id/editTextId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/MyCustomeEditTextStyle" />
BUT if you are want it to be globally set once and set without the xml because you are generating your EditText programmatically like this:
EditText editTextFieldTitle = new EditText(this);
you can then set your style in the application style like this:
<style name="AppTheme" parent="@android:style/YourParentTheme">
<item name="android:editTextStyle">@style/MyCustomeEditTextStyle</item>
</style>
答案2
得分: 0
我在这里找到了解决方案。https://stackoverflow.com/questions/11723881/android-set-view-style-programmatically
editTextFieldTitle = new EditText(new ContextThemeWrapper(getBaseContext(),R.style.input_fields_single_line),null,0);
英文:
I found the solution here. https://stackoverflow.com/questions/11723881/android-set-view-style-programmatically
editTextFieldTitle = new EditText(new ContextThemeWrapper(getBaseContext(),R.style.input_fields_single_line),null,0);
答案3
得分: 0
在EditText中有一个名为"styles"的属性,可以使用它。如果您想要自定义它,那么可以在drawable中创建自己的样式,创建一个新的XML文件,并通过使用styles属性将其链接到editText。
英文:
There is a attribute in EditText called styles use that.and if you want to customize it than make own style in drawable , A new XML and link to the editText using styles attribute.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论