所以我的sdk从15到21,当我打电话时setBackgroundDrawable(),Android Studio告诉我它已被弃用.
我想过用它来绕过它:
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.img_wstat_tstorm));
} else {
layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm));
}
Run Code Online (Sandbox Code Playgroud)
但是,我在"setBackground()"收到错误.
那么,你会如何应对呢?
有很多教程和SO的问题,实现自定义标题栏.但是,在我的自定义标题栏中,我有一个自定义渐变背景,我想知道如何在我的代码中动态设置它.
这是我的自定义标题栏被调用的地方:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.foo_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
Run Code Online (Sandbox Code Playgroud)
这是我的custom_title_bar:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@layout/custom_title_bar_background_colors">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/title_bar_logo"
android:gravity="center_horizontal"
android:paddingTop="0dip"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
如您所见,线性布局的背景由此人定义:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#616261"
android:endColor="#131313"
android:angle="270"
/>
<corners android:radius="0dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
我想要做的是在我的代码中动态设置这些渐变颜色.我不想像我们现在那样在我的XML文件中对它们进行硬编码.
如果您有更好的方法来设置背景渐变,我会对所有想法持开放态度.
先感谢您!!
我正在尝试为我的按钮创建自定义属性,但我不知道在属性声明中我必须使用哪种格式的图像...
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TCButton">
<attr name="Text" format="string"/>
<attr name="BackgroundImage" format="android:drawable" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
错误是格式="android:drawable"...
我正在使用新的TextInputLayout来包装EditText.当我确定某个字段有错误时,我会执行以下操作:
Drawable drawable = DrawableCompat.wrap(getEditText().getBackground());
DrawableCompat.setTintList(drawable, ColorStateList.valueOf(Color.RED));
Run Code Online (Sandbox Code Playgroud)
这适用于5.0并将下划线变为红色,但在4.4或4.1测试设备上不执行任何操作.我在这里错过了什么?看起来如此简单,根据谷歌"正常工作"...非常确定我也有最新版本:
编译'com.android.support:design:22.2.0'
FWIW,如果我使用setColorFilter而不是setTint,那么它可以在所有平台上运行,但是我有问题就会消失,一旦焦点设置/左/等等就不会回来......我更愿意这样做色调(如果有人在寻找额外的信用lol,我真的更喜欢将色调应用于焦点和非焦点状态)
谢谢!
我想使用view.setBackgroundDrawable(Drawable)但不推荐使用此方法.它被替换为.setBackground(Drawable).但我的最低API 8无法处理.它告诉我将最小值设置为API 16.
有没有办法根据设备的API使用不同的方法?
就像是
if(API<16)
{
view.setBackgroundDrawable(Drawable)
}
else
{
view.setBackground(Drawable)
}
Run Code Online (Sandbox Code Playgroud)
或者我真的必须更改最低API才能执行此操作吗?
这是EditText创建时的默认方式:

这是我改变它的背景后的样子:

要将背景更改为上面的图像,我将以下EditText属性添加到我的XML文件中的EditText:
android:background="@android:drawable/edit_text"
Run Code Online (Sandbox Code Playgroud)
我想要做的就是在我的活动课上做我上面做过的事情(给用户一个选择经典风格和透明/新风格的选项).
所以这样 - 我需要人们填写 /*...*/
if (classicTextbox()) { // the blocky white one
editText.setTextColor(Color.BLACK);
editText.setBackgroundResource(android.R.drawable.edit_text);
} else { // the new transparent one
editText.setTextColor(Color.WHITE);
editText.setBackgroundResource(/*...*/);
}
Run Code Online (Sandbox Code Playgroud)
那么如何将其更改回新的透明EditText样式?
我刚刚将我的Android项目的构建目标升级到API 17,现在我正在收到关于不推荐使用setBackgroundDrawable的警告.答案似乎是使用setBackground,但在旧版本中不可用.
使用新方法有什么实际优势,或Google是否只想更改名称?如果两者的工作方式相同,我认为使用平台版本检查或反射使我的代码复杂化没有任何意义.
以下代码:
View inflate = inflater.inflate(R.layout.page, null);
Drawable img = getResources().getDrawable((Integer) (item.get("img")));
inflate.findViewById(R.id.page_img).setBackground(img);
Run Code Online (Sandbox Code Playgroud)
产生以下错误:
java.lang.NoSuchMethodError: android.view.View.setBackground
我不知道为什么.我已经尝试过setBackground R.drawable.img但是我得到了同样的错误.