Customview Spinnerbutton, TextView ( . TextView).
, , android, , , .
Android , android styles.xml. .
package com.example.customviewattributes.p1;
import com.example.customviewattributes.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class Spinnerbutton extends TextView{
public Spinnerbutton(Context context) {
this(context, null);
}
public Spinnerbutton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Spinnerbutton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.Spinnerbutton,
0, 0
);
try {
int textStyleId = a.getResourceId(
R.styleable.Spinnerbutton_myTextAppearence, -1);
Log.i("................ID is",""+textStyleId);
this.setText("hello");
this.setTextAppearance(context,textStyleId);
} finally {
a.recycle();
}
}
}
styles.xml
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
<item name="android:textViewStyle">@style/QText</item>
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
<style name="QText" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">20sp</item>
<item name="android:textColor">@color/ccolor</item>
<item name="android:textStyle">bold</item>
<item name="android:typeface">sans</item>
</style>
</resources>
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Spinnerbutton">
<attr name="myTextAppearence" format="reference" />
</declare-styleable>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ccolor">#ff3232</color>
</resources>
.
Android
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<com.example.customviewattributes.p1.Spinnerbutton
android:id="@+id/sbp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
/>
</RelativeLayout>
MainActivity.java
setContentView(R.layout.activity_main);
com.example.customviewattributes.p1.Spinnerbutton cv = (Spinnerbutton) findViewById(R.id.sbp);
cv.setTextAppearance(this,R.style.QText1);
cv.setText("hello");
styles.xml
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
<item name="android:textViewStyle">@style/QText</item>
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
<style name="QText1" parent="@style/QText">
<item name="android:textSize">50sp</item>
<item name="android:textColor">@color/ccolor</item>
<item name="android:textStyle">bold</item>
<item name="android:typeface">sans</item>
</style>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ccolor">#4F47B7</color>
</resources>

,
