カスタムテキストビューで問題が発生しています。カスタムビューを作成しましたが、問題ありませんが、親の中央に配置しようとしています:
android:layout_centerHorizontal = true
しかし、それは親の中央にはありません。
android:layout_width="fill_parent"
これは、カスタム Textview の私のコードです。
public class GlowTextView extends TextView{
private Context context;
private AttributeSet attr;
private static final String COLOR = "#9bdb9d";
private static final String SHADOW_COLOR = "#26db2b";
public String ora = "00:00"; //il testo che verra' disegnato
/*handler per il ritardo dell' orologio*/
Handler handler = new Handler();
/*Calendar per il recupero dell' ora*/
private Calendar calendar;
private Paint paint = new Paint();
public GlowTextView(Context context) {
super(context);
this.context = context;
handler.post(sendData); //faccio partire il timer per l' ora
}
public GlowTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
this.attr = attrs;
handler.post(sendData);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
setText(ora);
//setTextSize(size); //grandezza del testo
setTextColor(Color.parseColor(COLOR));
setShadowLayer(15, 0, 0, Color.parseColor(SHADOW_COLOR));
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Neon.ttf")); //font per l' ora
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int reqWidth;
int reqHeight;
reqWidth = (int)(ora.length() * this.getTextSize());
reqHeight = (int) this.getTextSize();
// set the calculated width and height of your drawing area
setMeasuredDimension(reqWidth, reqHeight);
}
private void setClockText(String text) {
setText(text);
invalidate();
}
/*
* delay per l' aggiornamento dell' orologio
*/
private final Runnable sendData = new Runnable(){
public void run(){
try {
/*Recuper l' ora*/
calendar = Calendar.getInstance();
String minute = Integer.toString(calendar.get(Calendar.MINUTE));
if(minute.length() == 1) minute = '0' + minute;
String hour = Integer.toString(calendar.get(Calendar.HOUR_OF_DAY));
if(hour.length() == 1) hour = '0' + hour;
ora = hour + ":" + minute;
/*Passo l' ora alla textview
*/
setClockText(ora);
Log.d("Delay", "Refresh clock!");
handler.postDelayed(this, 15000);
}
catch (Exception e) {
e.printStackTrace();
}
}
};
}