Android ::设置后台从可绘制资源失败

时间:2014-08-13 15:40:29

标签: android android-shape

问题

我通过使用下面的代码扩展了一些布局以添加到现有布局中。

API = 19时似乎一切正常但是,19以下不是。

我不知道这里发生了什么,请帮助我找到问题。

示例图片

Example Image

在添加任何视图之前的默认视图

Before adding any view

示例代码

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.temp_added_layout, null, false);

    LinearLayout llFirstDevice = (LinearLayout) findViewById(R.id.llFirstDevice);
    if (android.os.Build.VERSION.SDK_INT < 16) {
        llFirstDevice.setBackgroundResource(R.drawable.temp_top_added);
    } else {
        llFirstDevice.setBackground(getResources().getDrawable(R.drawable.temp_top_added));
    }
    llFirstDevice.addView(v, 0);

在添加任何视图之前默认查看XML布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">


    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp" android:layout_weight="1" android:background="@drawable/temp_top_blank"
            android:gravity="center" android:id="@+id/llFirstDevice">
        <RelativeLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="fill_parent" android:gravity="center">
            <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" android:gravity="center">
                <ImageView
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:src="@drawable/ic_content_new" android:scaleType="fitXY"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentTop="true"/>
                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:text="A/C Node"
                        android:textSize="15sp"
                        android:textColor="#333333"
                        android:shadowColor="#7F000000"
                        android:shadowDx="-1"
                        android:shadowDy="-1"
                        android:shadowRadius="1" android:gravity="center" android:layout_alignParentLeft="true"
                        android:layout_alignParentTop="true"/>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp" android:layout_weight="1" android:background="@drawable/temp_mid_blank"
            android:id="@+id/llSecondDevice" android:gravity="center">

        <RelativeLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="fill_parent" android:layout_gravity="center_vertical">
            <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent"
                          android:layout_height="wrap_content" android:gravity="center"
                          android:layout_centerVertical="true" android:layout_alignParentLeft="true"
                          android:layout_alignParentStart="true">
                <ImageView android:layout_width="30dp" android:layout_height="30dp"
                           android:src="@drawable/ic_content_new" android:scaleType="fitXY"
                           android:layout_alignParentLeft="true" android:layout_alignParentTop="true"/>
                <TextView android:layout_width="wrap_content" android:layout_height="match_parent"
                          android:text="A/C Node" android:textSize="15sp"
                          android:textColor="#333333" android:shadowColor="#7F000000" android:shadowDx="-1"
                          android:shadowDy="-1" android:shadowRadius="1" android:gravity="center"
                          android:layout_alignParentLeft="true" android:layout_alignParentTop="true"/>
            </LinearLayout>
        </RelativeLayout>

    </LinearLayout>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp" android:layout_weight="1" android:background="@drawable/temp_bot_blank"
            android:gravity="center" android:id="@+id/llThirdDevice">
        <RelativeLayout android:orientation="horizontal" android:layout_width="match_parent"
                        android:layout_height="fill_parent" android:gravity="center">
            <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent"
                          android:layout_height="wrap_content" android:gravity="center">
                <ImageView android:layout_width="30dp" android:layout_height="30dp"
                           android:src="@drawable/ic_content_new" android:scaleType="fitXY"
                           android:layout_alignParentLeft="true" android:layout_alignParentTop="true"/>
                <TextView android:layout_width="wrap_content" android:layout_height="match_parent"
                          android:text="A/C Node" android:textSize="15sp"
                          android:textColor="#333333" android:shadowColor="#7F000000" android:shadowDx="-1"
                          android:shadowDy="-1" android:shadowRadius="1" android:gravity="center"
                          android:layout_alignParentLeft="true" android:layout_alignParentTop="true"/>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

R.Drawable.temp_top_added

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <solid android:color="#7e3f9e" />
        </shape>
    </item>
    <item
            android:left="20dp"
            android:top="10dp"
            android:right="20dp"
            android:bottom="10dp">
        <shape >
            <stroke
                    android:dashGap="10dp"
                    android:dashWidth="10dp"
                    android:width="2dp"
                    android:color="#99333333" />
            <padding
                    android:bottom="@dimen/temp_added_padding_B"
                    android:left="30dp"
                    android:right="30dp"
                    android:top="@dimen/temp_added_padding_T" />
        </shape>
    </item>
</layer-list>

1 个答案:

答案 0 :(得分:0)

只需将您的形状编辑为:

<shape >
    <stroke
        android:dashGap="10dp"
        android:dashWidth="10dp"
        android:width="2dp"
        android:color="#99333333" />
    <padding
        android:bottom="@dimen/temp_added_padding_B"
        android:left="30dp"
        android:right="30dp"
        android:top="@dimen/temp_added_padding_T" />
    <solid android:color="@android:color/transparent"/> <!-- ADD THIS LINE -->
</shape>