0

如果我只保留并使用以'android:'开头的属性,那么一切都运行良好。当我为attribute(在本例中为<attr name="detailsBackground"/>)添加自定义名称时,视图中的任何内容都不再起作用。会发生以下情况:

  • 班上所有的R都是红色的
  • 在我尝试构建后,我收到此消息:

错误:(10) 在包 'com.company.package' 中找不到属性 'detailsBackground' 的资源标识符

这就是我在attrs.xml 中的内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CurrentMonthPanels">
        <attr name="android:background"/>
        <attr name="android:src"/>
        <attr name="android:text"/>
        <attr name="detailsBackground"/>
    </declare-styleable>
</resources>

这是我的自定义布局:

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

    <RelativeLayout
        android:id="@+id/mainBackground"
        android:layout_width="match_parent"
        android:layout_height="110dp"
        android:background="@drawable/full_panel_red"
        android:padding="10dp">

        <ImageView
            android:id="@+id/iconImage"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:src="@mipmap/expenses_icon" />

        <LinearLayout
            android:layout_width="350dp"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:gravity="end"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/my_white"
                android:text="160000"
                android:textSize="50sp"
                android:gravity="end"/>

            <TextView
                android:id="@+id/textDescription"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="end"
                android:textColor="@color/my_white"
                android:textSize="20sp"
                android:text="Expenses this month (USD)" />

        </LinearLayout>

    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/detailsLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/full_panel_red_transparent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_margin="5dp"
            android:text="@string/quick_details"
            android:textColor="@color/my_red"
            android:textSize="17dp"
            android:gravity="center"
            android:padding="10dp"/>

        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@mipmap/arrow_right"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:tint="@color/my_red"/>

    </RelativeLayout>

</LinearLayout>

这是我的CustomMonthPanels 类

public class CurrentMonthPanels extends LinearLayout {

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public CurrentMonthPanels(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs, 
                       R.styleable.CurrentMonthPanels);

        Drawable iconToShow = 
           a.getDrawable(R.styleable.CurrentMonthPanels_android_src);
        Drawable mainBackground = 
           a.getDrawable(R.styleable.CurrentMonthPanels_android_background);
        Drawable detailsBackground = 
           a.getDrawable(R.styleable.CurrentMonthPanels_detailsBackground);
        String textDescription = 
           a.getString(R.styleable.CurrentMonthPanels_android_text);
        a.recycle();

        inflate(context, R.layout.current_month_panel, this);

        RelativeLayout mainLayout = (RelativeLayout) 
                              findViewById(R.id.mainBackground);
        mainLayout.setBackground(mainBackground);

        ImageView iconImage = (ImageView) findViewById(R.id.iconImage);
        iconImage.setImageDrawable(iconToShow);

        TextView textDescriptionView = (TextView) 
                              findViewById(R.id.textDescription);
        textDescriptionView.setText(textDescription);

        RelativeLayout detailsLayout = (RelativeLayout) 
                              findViewById(R.id.detailsLayout);
        detailsLayout.setBackground(detailsBackground);
    }

}

这是我试图用自定义属性显示视图的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:namespace="http://schemas.android.com/apk/res-auto"
    tools:context="com.company.package.CurrentMonthPanels"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <com.company.package.CurrentMonthPanels
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/full_panel_red"
        android:src="@mipmap/ic_launcher"
        android:text="Dasaa"
        namespace:detailsBackground="@drawable/full_panel_red_transparent" 
/>

</RelativeLayout>
4

0 回答 0