Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 文本视图';单击时,s角变为白色_Android_Textview_Statelistdrawable - Fatal编程技术网

Android 文本视图';单击时,s角变为白色

Android 文本视图';单击时,s角变为白色,android,textview,statelistdrawable,Android,Textview,Statelistdrawable,我使用以下代码片段显示TextView: super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView)findViewById(R.id.test); GradientDrawable gd = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{0xffff0000,0xffff0000}); g

我使用以下代码片段显示
TextView

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView)findViewById(R.id.test);

GradientDrawable gd = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{0xffff0000,0xffff0000});
gd.setCornerRadius(10);
GradientDrawable gd1 = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{0xff00ff00,0xff00ff00});
gd1.setCornerRadius(10);
StateListDrawable background = new StateListDrawable();
background.addState(new int[]{android.R.attr.state_pressed}, gd1);
background.addState(StateSet.WILD_CARD, gd);
tv.setBackgroundDrawable(background);
tv.setClickable(true);
在正常状态下,其外观正常:

但单击时,四个角将变为白色,请参见:


如何避免这种情况?

在可绘图文件夹下创建一个text\u bg\u selector.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:state_pressed="true"
            android:drawable="@drawable/pressed_bg" /> <!-- pressed -->
        <item android:state_focused="true"
            android:drawable="@drawable/focused_bh" /> <!-- focused -->
        <item android:drawable="@drawable/default_bg" /> <!-- default -->
</selector>
编辑您的代码:

由于setBackgroundDrawable已弃用,您必须检查它并按如下方式编写setBackground:

GradientDrawable gd = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{0xffff0000,0xffff0000});
gd.setCornerRadius(10);
GradientDrawable gd1 = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{0xff00ff00,0xff00ff00});
gd1.setCornerRadius(10);
StateListDrawable background = new StateListDrawable();
background.addState(new int[]{android.R.attr.state_pressed}, gd1);
background.addState(new int[]{android.R.attr.state_focused}, gd1);
background.addState(StateSet.WILD_CARD, gd);
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    t.setBackgroundDrawable(background);
} else {
    t.setBackground(background);
}
t.setClickable(true);
GradientDrawable gd=新的GradientDrawable(Orientation.TOP_BOTTOM,new int[]{0xffff0000,0xffff0000});
gd.设置转弯半径(10);
GradientDrawable gd1=新的GradientDrawable(Orientation.TOP_-BOTTOM,new int[]{0xff00ff00,0xff00ff00});
gd1.设置转弯半径(10);
StateListDrawable后台=新StateListDrawable();
addState(新int[]{android.R.attr.state_pressed},gd1);
addState(新int[]{android.R.attr.state_-focused},gd1);
背景.addState(StateSet.WILD_CARD,gd);
int sdk=android.os.Build.VERSION.sdk\u int;
if(sdk
为文本视图添加选择器你是什么意思?你能给我举个例子吗?我不能使用xml资源,因为颜色不是固定值。顺便说一句,我认为在运行时使用xml或构建后台没有区别。还是我遗漏了什么?不是真的。Just setBackgroundDrawable已弃用,因此您必须执行SDK检查。将更新我的答案。新的API
setBackground
只调用
setBackgroundDrawable
,所以我不认为它会导致这种奇怪的行为。好吧,这段代码在每一个版本中都非常适合我(在8、11和17个设备上试用过)。背景应该不会有任何问题,就像我写的那样,因为它工作正常。可能是你没有放在这里的其他代码导致了这个问题。我应该意识到这一点…无论如何,谢谢
GradientDrawable gd = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{0xffff0000,0xffff0000});
gd.setCornerRadius(10);
GradientDrawable gd1 = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{0xff00ff00,0xff00ff00});
gd1.setCornerRadius(10);
StateListDrawable background = new StateListDrawable();
background.addState(new int[]{android.R.attr.state_pressed}, gd1);
background.addState(new int[]{android.R.attr.state_focused}, gd1);
background.addState(StateSet.WILD_CARD, gd);
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    t.setBackgroundDrawable(background);
} else {
    t.setBackground(background);
}
t.setClickable(true);