Android以编程方式创建了textview setBackgroundResource,无法在设备中运行

时间:2015-05-15 05:29:17

标签: android

代码----

private TextView SetControlForDetails(TextView a_tv_Detail,
        int ai_MarginLeft, int ai_MarginTop, int ai_Width,
        String as_Detail, RelativeLayout a_rlDetail,
        int ai_BackgroundResources) 
{
    String s_MethodName = "SetControlForDetails ";
    try {

         a_tv_Detail = new TextView(MainActivity.this);
                     LayoutParams Layout_Params = new LayoutParams(ai_Width, 50);
         Layout_Params.leftMargin = ai_MarginLeft;
         Layout_Params.topMargin = ai_MarginTop;
        a_tv_Detail.setLayoutParams(Layout_Params);
        a_tv_Detail.setText(as_Detail);
        a_tv_Detail.setGravity(Gravity.CENTER);
        a_tv_Detail.setTextColor(getResources().getColor(R.color.white));
        a_tv_Detail.setTypeface(null, Typeface.BOLD);
        a_tv_Detail.setBackgroundResource(getResources().getIdentifier("header_bg", "drawable",getPackageName());           
        a_rlDetail.addView(a_tv_Detail);
    } catch (Exception e) {
        Log.v(s_MethodName, e.getMessage());
        e.printStackTrace();
    }
    return a_tv_Detail;

}

xml ----

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <stroke
        android:width="5dp"
        android:color="@color/border_color" />

    <solid 
        android:color="@color/headerbg_color" >
    </solid>

    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />

</shape>

1 个答案:

答案 0 :(得分:0)

试试这个

 a_tv_Detail.setBackgroundResource(R.drawable.header_bg);