Skip to content

Instantly share code, notes, and snippets.

@Palatis
Created April 17, 2016 14:07
Show Gist options
  • Save Palatis/7e7955cc82a4dcbd1b941d011fbdaeda to your computer and use it in GitHub Desktop.
Save Palatis/7e7955cc82a4dcbd1b941d011fbdaeda to your computer and use it in GitHub Desktop.
public class MyCustomView extends View {
public MyCustomView(Context context) { this(context, R.style.MyCustomView); }
public MyCustomView(Context context, AttributeSet attrs) { this(context, attrs, 0); }
public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final int defStyleRes = 0;
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyle, defStyleRes);
try {
this.search_width = a.getDimension(R.styleable.MyCustomView_search_width, 123); // a fallback default value...
this.search_height = a.getDimension(R.styleable.MyCustomView_search_height, 456);
/* ... i'm lazy to type the rest...... */
} finally {
/* call recycle() after you're done. */
a.recycle();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyCustomView">
<item name="search_width">300dp</item>
<item name="search_height">50dp</item>
<item name="f1_column_width">25dp</item>
<item name="f3_column_width">280dp</item>
<item name="f4_column_width">45dp</item>
<item name="edittext_height">30dp</item>
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the name doesn't have to be the same as the class name of your
custom view, but we usually use the same name to avoid obfuscation. -->
<declare-styleable name="MyCustomView">
<attr name="search_width" format="dimension"/>
<attr name="search_height" format="dimension"/>
<attr name="f1_column_width" format="dimension"/>
<attr name="f3_column_width" format="dimension"/>
<attr name="f4_column_width" format="dimension"/>
<attr name="edittext_height" format="dimension"/>
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyCustomView">
<item name="search_width">150dp</item>
<item name="search_height">50dp</item>
<item name="f1_column_width">25dp</item>
<item name="f3_column_width">140dp</item>
<item name="f4_column_width">45dp</item>
<item name="edittext_height">30dp</item>
</declare-styleable>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment