2013-08-07 85 views
0

我遇到了自定義textview的麻煩。 我創建的自定義視圖,它是好的,但現在我想用居中它的父:自定義textview不填充父項

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(); 
     } 
    } 
}; 
} 
+0

你甚至需要重寫onMeasure你的情況嗎?如果你沒有覆蓋onMeasure,你的問題會是什麼? – Karakuri

+0

如果我沒有覆蓋它的問題是相同的textview不集中 – TheRedFox

回答

0

嘗試調整佈局的重心

如果你的TextView有layout_width = 「match_parent」

<package.name.customTextView 
     android:id="@+id/myCustomTextView" 
     android:gravity="center_horizontal" 
     ... 

如果你的TextView有layout_width = 「WRAP_CONTENT」

<package.name.customTextView 
     android:id="@+id/myCustomTextView" 
     android:layout_gravity="center_horizontal" 
     ... 

希望這有助於編碼快樂!

+0

非常感謝你對我有用! – TheRedFox

+0

很高興能幫到你! – MattMatt