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