Android 背景可绘制的图形';t将其大小与其父布局匹配

Android 背景可绘制的图形';t将其大小与其父布局匹配,android,Android,我在将背景色应用于列表项布局时遇到问题。我正在做一个学习android的示例应用程序。我的问题是它显示彩色背景,但它不是列表项布局的大小。它有点小。由于某些原因,它不起作用,这里的每个人都建议设置一个可绘制的背景,如下所示: view.setBackgroundResource(R.drawable.ic_green); <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.an

我在将背景色应用于列表项布局时遇到问题。我正在做一个学习android的示例应用程序。我的问题是它显示彩色背景,但它不是列表项布局的大小。它有点小。由于某些原因,它不起作用,这里的每个人都建议设置一个可绘制的背景,如下所示:

view.setBackgroundResource(R.drawable.ic_green);
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#FFFFCC" />

</shape>
我在
CursorAdapter.bindView(视图视图、上下文、光标)中执行此操作。因为我当然是在处理清单。每个项目都有相应的颜色数据,我需要在每个列表项目上反映这些数据。我需要以编程的方式来做这件事。因为我必须允许自己随意改变背景色

看起来是这样的:

view.setBackgroundResource(R.drawable.ic_green);
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#FFFFCC" />

</shape>

我的可提取资源如下:

view.setBackgroundResource(R.drawable.ic_green);
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#FFFFCC" />

</shape>

我的列表项布局如下所示:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:layout_margin="5sp"
    android:background="@drawable/note_style"
    android:padding="5sp"
    android:paddingLeft="10sp"
    android:paddingRight="10sp" >

    <TextView
        android:id="@+id/textview_note_short"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:gravity="start"
        android:maxLength="18"
        android:maxLines="1"
        android:text="@string/text_sample_short_note"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/textview_note_remind_every"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/textview_note_short"
        android:layout_alignStart="@id/textview_note_short"
        android:layout_below="@id/textview_note_short"
        android:text="@string/text_sample_remind_every"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/textview_note_date_created"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@id/textview_note_short"
        android:text="@string/text_sample_date"
        android:textSize="12sp" />

    <ImageView
        android:id="@+id/imageview_note_notification_alarm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/textview_note_remind_every"
        android:layout_alignRight="@id/textview_note_date_created"
        android:layout_alignEnd="@id/textview_note_date_created"
        android:adjustViewBounds="true"
        android:contentDescription="@string/content_description_past_due"
        android:maxHeight="20dp"
        android:maxWidth="20dp"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_action_alarm" />

    <ImageView
        android:id="@+id/imageview_note_notification_due"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/imageview_note_notification_alarm"
        android:layout_toLeftOf="@id/imageview_note_notification_alarm"
        android:layout_toStartOf="@id/imageview_note_notification_alarm"
        android:adjustViewBounds="true"
        android:contentDescription="@string/content_description_past_due"
        android:maxHeight="20dp"
        android:maxWidth="20dp"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_action_due" />

</RelativeLayout>

它会随心所欲地改变背景颜色,但正如您在图片上看到的那样,绿色背景不会击中布局的边缘,而布局的边缘应该与屏幕的宽度和高度相匹配


有什么想法吗?谢谢大家!

为什么要使用
sp
作为填充?如果你只是想让背景成为某种颜色,不确定为什么需要可绘制的。在列表项XML的
RelativeLayout
块中,放入
android:background=“#FFFFCC”
。这就是你想要实现的吗?不,我想通过编程实现。你只是尝试了
setBackgroundColor
?什么灰色?例如,当您使用setBackgroundColor(Color.RED)时?为什么要使用
sp
作为填充?如果您只是希望背景为特定颜色,则不确定为什么需要可绘制背景。在列表项XML的
RelativeLayout
块中,放入
android:background=“#FFFFCC”
。这就是你想要实现的吗?不,我想通过编程实现。你只是尝试了
setBackgroundColor
?什么灰色?例如,当您使用setBackgroundColor(Color.RED)时?