Beispiel arbeiten sollte benutzerdefinierte Ansicht, die einen eigenen Standardstil hat. In diesem Beispiel wird die benutzerdefinierte Ansicht PasswordGrid erweitert GridLayout. Ich habe einen Stil für PasswordGrid angegeben, der ein Hintergrundbild mit dem Standard-Android-Attribut android: background festlegt.
public class PasswordGrid extends GridLayout {
public PasswordGrid(Context context) {
super(context);
init(context, null, 0);
}
public PasswordGrid(Context context, AttributeSet attrs) {
super(context, attrs, R.attr.passwordGridStyle);
init(context, attrs, 0);
}
public PasswordGrid(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
if (!isInEditMode()) {
TypedArray stdAttrs = context.obtainStyledAttributes(attrs,
new int[] { android.R.attr.background }, // attribute[s] to access
defStyle,
R.style.PasswordGridStyle); // Style to access
// or use any style available in the android.R.style file, such as
// android.R.style.Theme_Holo_Light
if (stdAttrs != null) {
Drawable bgDrawable = stdAttrs.getDrawable(0);
if (bgDrawable != null)
this.setBackground(bgDrawable);
stdAttrs.recycle();
}
}
}
Hier ist ein Teil meiner Stile.XML-Datei:
<declare-styleable name="passwordGrid">
<attr name="drawOn" format="color|reference" />
<attr name="drawOff" format="color|reference" />
<attr name="pathWidth" format="integer" />
<attr name="pathAlpha" format="integer" />
<attr name="pathColor" format="color" />
</declare-styleable>
<style name="PasswordGridStyle" parent="@android:style/Widget.GridView" >
<!-- Style custom attributes. -->
<item name="drawOff">@drawable/ic_more</item>
<item name="drawOn">@drawable/ic_menu_cut</item>
<item name="pathWidth">31</item>
<item name="pathAlpha">129</item>
<item name="pathColor">@color/green</item>
<!-- Style standard attributes -->
<item name="android:background">@drawable/pattern_bg</item>
</style>
Das gleiche Problem mit der Galerie-Ansicht Tutorial existiert, habe ich Abhilfen gesehen, dass das Tutorial Arbeit machen, aber keine Erklärung, wie das Tutorial würde nur mit SDK-Klassen werden muß, festgelegt und nicht geben Sie Ihre eigenen Xml mit dem Styleable drin. Das Tutorial ist unter http://developer.android.com/resources/tutorials/views/hello-gallery.html der Code ist in der "ImageAdapter (Context c)" Constructor – AGrunewald
Hier ist eine ähnliche Diskussion http://StackOverflow.com/q/8793183/1307690 – Lemberg