android自定义arrayadapter以编程方式添加视图,后台无法正常工作

时间:2014-08-08 08:23:15

标签: android

我有一个用于listview的自定义数组适配器,其中我尝试根据某些值为每个项添加背景。但背景永远不会添加。有谁知道为什么会这样?

这是我的自定义数组适配器:

@Override
public View getView(int p, View convertView, ViewGroup parent) {

    RelativeLayout row = new RelativeLayout(context);

    LinearLayout.LayoutParams rowParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, context.getResources().getDisplayMetrics());
    int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, context.getResources().getDisplayMetrics());

    RelativeLayout bubble = new RelativeLayout(context);
    RelativeLayout.LayoutParams boubleParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    bubble.setPadding(padding, padding, padding, padding);

    if (items.get(p).deviceId == deviceId) {
        boubleParams.setMargins(margin, 0, 0 ,0);
        boubleParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        bubble.setBackgroundResource(R.drawable.chat_bubble_right);
        convertView.setBackgroundResource(R.drawable.chat_bubble_right);
    } else {
        boubleParams.setMargins(0, 0, margin ,0);
        boubleParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        bubble.setBackgroundResource(R.drawable.chat_bubble_left);
        convertView.setBackgroundResource(R.drawable.chat_bubble_left);
    }

    TextView message = new TextView(context);
    message.setText(items.get(p).message);
    message.setGravity(Gravity.CENTER_VERTICAL);
    message.setTextSize(16);
    message.setTypeface(Font.rLight(context));
    message.setId(75014711);

    RelativeLayout.LayoutParams messageParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    TextView time = new TextView(context);
    time.setText(DateUtils.getRelativeTimeSpanString(items.get(p).created*1000, System.currentTimeMillis(), 0));
    time.setGravity(Gravity.LEFT);
    time.setGravity(Gravity.CENTER_VERTICAL);
    time.setTextSize(12);
    time.setTypeface(Font.rLight(context));

    RelativeLayout.LayoutParams timeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    timeParams.addRule(RelativeLayout.BELOW, 75014711);
    timeParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    if (items.get(p).deviceId == deviceId) {
        timeParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        messageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        message.setGravity(Gravity.RIGHT);
    } else {
        timeParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        messageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        message.setGravity(Gravity.LEFT);
    }

    bubble.addView(message, messageParams);
    bubble.addView(time, timeParams);

    row.addView(bubble, boubleParams);
    return row;
}

我尝试添加的背景位于drawable文件夹中,如下所示:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#cccccc" />
<corners
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="10dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="0dp" /></shape>

我错过了什么吗?

编辑:使用api级别20.似乎我不是唯一的一个:Button's background drawable (shape) won't show up unless app API is 20+ (Android L)

0 个答案:

没有答案