如何在Holo主题Api 11-17 Android中以编程方式将EditText背景设置为默认值

时间:2013-08-07 10:04:36

标签: java android android-layout android-edittext

因为我正在使用

EditText et=new EditText(this);
locationEditText.setBackgroundDrawable(et.getBackground());

但是在{16}中不推荐使用setBackgroundDrawable(Drwable),因为无法使用setBackground(Drwable),因为它是在api 16中添加的,我使用的是早于版本的

所以只有两个功能留给我

是:setBackgroundColor(int color)setBackgroundResource(int resid)

那么如何将它设置为Holo Light Theme中的默认EditText

3 个答案:

答案 0 :(得分:1)

使用执行setBackgroundDrawable(Drawable)相同工作的setBackground(Drawable drawable)

编辑:感谢Warp It代码(source)

EditText et=new EditText(this);

int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    setBackgroundDrawable(et.getBackground());
} else {
    setBackground(et.getBackground());
}

您可以找到更多信息in this question

答案 1 :(得分:0)

这可能对您有所帮助

<EditText
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    style="@android:style/Widget.Holo.EditText"/>

答案 2 :(得分:0)

我的建议是为edittext创建一个模板布局xml文件,其中包含您需要分配的样式,并在代码中需要时对其进行充气。 在XML中          

在代码中使用它:

  

EditText et =(EditText   )getLayoutInflater()。inflate(R.layout.edtemplate,null);

更多地参考 android set style in code