怎么样才能在Android中从styles.xml中设置EditText的样式?

huangapple go评论88阅读模式
英文:

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

&lt;style name=&quot;input_fields_single_line&quot;&gt;
&lt;item name=&quot;android:padding&quot;&gt;8dp&lt;/item&gt;
&lt;item name=&quot;android:background&quot;&gt;@drawable/input_field_shape&lt;/item&gt;
&lt;item name=&quot;android:maxLines&quot;&gt;1&lt;/item&gt;
&lt;/style&gt;

Java code:

 List&lt;EditText&gt; inputFieldList = new ArrayList&lt;EditText&gt;();
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:

&lt;style name=&quot;MyCustomeEditTextStyle&quot; parent=&quot;@android:style/Widget.EditText&quot;&gt;
    ...add as many items as you need
    &lt;item name=&quot;android:padding&quot;&gt;8dp&lt;/item&gt;
    &lt;item name=&quot;android:background&quot;&gt;@drawable/input_field_shape&lt;/item&gt;
    &lt;item name=&quot;android:maxLines&quot;&gt;1&lt;/item&gt;
&lt;/style&gt;

Then call your style inside each edit text in the xml like the following:

&lt;EditText
    android:id=&quot;@+id/editTextId&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    style=&quot;@style/MyCustomeEditTextStyle&quot; /&gt;

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:

&lt;style name=&quot;AppTheme&quot; parent=&quot;@android:style/YourParentTheme&quot;&gt;
    &lt;item name=&quot;android:editTextStyle&quot;&gt;@style/MyCustomeEditTextStyle&lt;/item&gt;
&lt;/style&gt;

答案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.

huangapple
  • 本文由 发表于 2020年9月12日 21:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63860849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定