当前位置: 首页 > 知识库问答 >
问题:

尝试在适配器中将BackgroundColor设置为drawable,但未按预期工作

富凯旋
2023-03-14

我将linearlayout背景设置为可绘制文件,以便创建圆角外观:

<LinearLayout
    android:id="@+id/layout_top"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@drawable/rectangle_topcorner">

     <TextView android:layout_width="fill_parent"
        android:id="@+id/textView1"
        android:layout_height="wrap_content"
        android:text="Impact"
        android:layout_gravity="center"
        android:textColor="@android:color/white"
        android:layout_marginLeft="10dip"  />
</LinearLayout>

在我的适配器中,在我膨胀布局项后,我正在尝试将背景颜色更改为“不同”的可绘制对象,以更改背景颜色:

LinearLayout top = (LinearLayout) view.findViewById(R.id.layout_top);
top.setBackgroundColor(R.drawable.rectangle_topcorner_high);

问题是,这样做后,矩形失去了圆角外观,只是一个普通的老正方形。

两个可绘制的:矩形_上角

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:id="@+id/background_shape" >
<corners android:topLeftRadius="30dp"
    android:topRightRadius="30dp" />
<solid android:color="#005577"/>  
</shape>

矩形_上角_高

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:id="@+id/background_shape" >
<corners android:topLeftRadius="30dp"
    android:topRightRadius="30dp" />
<solid android:color="#83162D"/>  
</shape>

我遗漏了一些保留圆角的东西?

共有2个答案

空浩淼
2023-03-14

我想我会分享我最终的解决方案——因为我正在尝试

top.setBackgroundColor(R.drawable.rectangle_topcorner_high);

正确的(工作)代码是:

top.setBackground(context.getResources().getDrawable(R.drawable.rectangle_topcorner_high));
农永宁
2023-03-14

尝试使用:

top.setBackgroundResource(R.drawable.rectangle_topcorner_high);

setBackgroundColor用于颜色资源

 类似资料:
  • 当我尝试放置一个带有十六进制颜色的String变量来设置适配器类中edittext的时,它说 StringIndexOutofBoundsException,长度=0,索引=0。 当我手动将字符串插入中时,例如,它就能工作! 我的适配器类:

  • 我最近更新了我的Spring启动版本从2.3.0。发布到2.4.4,还更新了地图结构版本从1.3.1到最新版本。最终版本是1.4.2。最终版本。使用的lombok版本-1.18.18 但是其中一个在1.3.1上运行良好的映射器在1.4.2上不再工作,场景如下。 POM: 实体: 域名: 制图员: 使用MapStruct 1.3.1生成的代码 用Map结构1.4.2生成代码 正如您在1.4.2版本中

  • 我正在使用spring Roo并希望访问Controller类中的一个bean,该类在ApplicationContext.xml中具有以下配置: 配置类本身是: 在我的Controller中,我认为一个简单的Autowired注释应该可以完成这项工作 在启动过程中,spring在setSkipWeeks方法中打印消息。不幸的是,每当我在控制器中调用config.getSkipWeeks()时,它

  • 当我运行以下程序时,它只打印 然而,从Java 8的equalsIgnoreCase文档中我们发现: 如果以下至少一项为真,则两个字符c1和c2被视为相同的忽略情况: •对每个字符应用java.lang.character.ToUpperCase(char)方法会产生相同的结果 所以我的问题是为什么这个程序不打印 在这两种操作中,都使用了大写字符。

  • 我试图使用来传输我根据前面的问题设置的自定义标头。 我在文件中读到... 我的属性包括: