Problem mit Custoum Views und Custom Attributes

  • 2 Antworten
  • Letztes Antwortdatum
V

Viper2000

Ambitioniertes Mitglied
4
Hallo,

habe folgendes Problem:

Eine Activity enthält folgendes Layout (Auszug):

Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.deeds.saunaconnection"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >



    <com.deeds.Gui.VintageHandMeterView
        android:id="@+id/vintageHandMeterView"
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="30dp"
        custom:centerPosition="50"
        custom:maximumPosition="110"
        custom:minimumPosition="-10"
        custom:titleText="Temperatur in °C" />
        
            <com.deeds.Gui.VintageHandMeterView
        android:id="@+id/vintageHandMeterView1"
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="30dp"
        android:layout_marginTop="30dp"
        custom:centerPosition="50"
        custom:maximumPosition="110"
        custom:minimumPosition="-10"
        custom:titleText="Luftfeuchtigkeit in °%" />

</RelativeLayout>

es werden also zwei VintageHandMeter Views erstellt. Dies sind selbst programierte Views die ein Zeigerinstrument darstellen. Über die custom: Attribute aus der attrs.xml kann das Objekt konfiguriert werden. Diese werden an den Konstruktor übergeben. Einmal der Titel Luftfeuchtigkeit und einmal Temperatur.

Hier die attrs.xml:

Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="VintageHandMeterView">
        <attr name="centerPosition" format="integer"/>
        <attr name="minimumPosition" format="integer"/>
        <attr name="maximumPosition" format="integer"/>
        <attr name="titleText" format="string"/>
    </declare-styleable>    
</resources>

Das funktioniert auch insofern, dass zwei der Views angezeigt werden aber beide z.B. den Titel "Luftfeuchtigkeit in %" haben. anschienend werden die custom attribute aus dem ersten VintagehandmeterView ignoriert.

Hier noch der Konstruktor der VintageHandMeterview Klasse:

Code:
public VintageHandMeterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        
        //Eigene Skalen-Werte aus der XML-Datei attrs.xml bzw der layout xml datei auslesen
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VintageHandMeterView);
        for (int i=0; i < a.getIndexCount(); ++i)
        {
            int attr = a.getIndex(i);
            switch (attr)
            {
                case R.styleable.VintageHandMeterView_centerPosition:
                    centerPosition = a.getInteger(attr, 50);
                    break;
                    
                case R.styleable.VintageHandMeterView_minimumPosition:
                    minPosition = a.getInteger(attr, 0);
                    break;
                    
                case R.styleable.VintageHandMeterView_maximumPosition:
                    maxPosition = a.getInteger(attr, 100);
                    break;
                    
                case R.styleable.VintageHandMeterView_titleText:
                    titleText = a.getString(attr);
                    break;
            }
        }
        //Falls kein Titeltext im XML definiert wurde
        if (titleText == "")
        {
            //Der Titeltext des Views soll ueber das XML layout file gesetzt werden (in Verbindung mit attrs.xml)
            titleText = "titleText not defined in XML - please define";
        }
        a.recycle();
        
        this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        prepareDrawingTools();
        Log.d(TAG, "Konstruktor b");
    }


Hat jemand eine Idee oder Vermutung woran das liegen könnte?
Wäre über Hilfe sehr dankbar,

Viele Grüße
Viper2000
 
Problem hat sich erledigt. Die oben genannte Konfiguration ist soweit völlig in Ordnung. Der Fehler leg in der VintageHandMeterview Klasse selbst. In dieser hatte ich die Klassenvariablen (titleText, centerPosition, ...) als static deklariert. Das ist dann ein Problem beim Instanzieren. Nach entfernen des static Schlüsselwortes funktioniert nun alles.:thumbup:
 
I prefer to read this kind of nice stuff. The quality of content is fine and the conclusion is good. Thanks for the post.
 
Zurück
Oben Unten