2017-10-19 153 views
-1

如何創建一個類的對象,並提供價值構造器一樣的AttributeSet等,並且還的setText(「」)如何創建CustomTextView類的對象

public class CustomTextView extends AppCompatTextView { 

    public CustomTextView(Context context, AttributeSet attributeSet) { 
     super(context, attributeSet); 
     paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     paint1 = new Paint(Paint.ANTI_ALIAS_FLAG); 
     paint2 = new Paint(Paint.ANTI_ALIAS_FLAG); 
     textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); 
     textPaint.setTextSize(getTextSize()); 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 
     TextPaint textPaint = getPaint(); 
     // .......... 
    } 
} 

//如何在Java中使用

CustomTextView cTV = new CustomTextView (...........)?? 
cTV.setText("how to create like that"); 
+0

你想從你的java代碼創建一個新的'CustomTextView'對象?或xml佈局? – pskink

+1

在developer.android.com上有自定義視圖的完整指南請參閱https://developer.android.com/training/custom-views/index.html – Fusselchen

+0

擁有這種構造方法意味着可以使用此從'xml'佈局定製視圖,沒有直接的方式通過java代碼動態創建。 – azizbekian

回答

0

我只是創建構造CustomTextView類

public CustomTextView(Context context) { 
     this(context, null); 

} 

現在我們可以輕鬆訪問CustomTextView Class的任何屬性。

2

首先你需要定義這個類。

public class CustomFontTextView extends AppCompatTextView { 

private static final String CUSTOM_FONT = "cfont"; 
private static final String FONT_PATH = "fonts/"; 

private String ttfName; 
private Typeface font; 
private CharSequence text; 
private BufferType type; 

public CustomFontTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context, attrs); 
} 

public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(context, attrs); 
} 


private void init(Context context, AttributeSet attrs) { 

    this.ttfName = attrs.getAttributeValue(AppConstants.NAMESPACE, CUSTOM_FONT); 

    if (ttfName.startsWith("@string/") || ttfName.startsWith("@")) { 
     TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView); 
     this.ttfName = ta.getString(R.styleable.CustomFontTextView_cfont); 
     ta.recycle(); 
    } 

    this.setPaintFlags(this.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG); 

    try { 
     font = Typeface.createFromAsset(context.getAssets(), FONT_PATH + ttfName); 
     setTypeface(font); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     font = Typeface.defaultFromStyle(Typeface.NORMAL); 
     setTypeface(font); 
    } 

    setText(text, type); 
} 


@Override 
public void setText(CharSequence text, BufferType type) { 
    try { 
     this.text = text; 
     this.type = type; 
     if (font == null) 
      return; 

     CustomFontStyling customFontStyling = new CustomFontStyling(getContext(), font); 
     super.setText(customFontStyling.getCustomText(text.toString()), type); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

}

後,您需要添加設置樣式在attrs.xml

<declare-styleable name="CustomFontTextView"> 
    <attr name="cfont" format="string" /> 
</declare-styleable> 

終於在你的XML佈局

<YOURAPPPACKAGE.CustomFontTextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:cfont="@string/open_sans_regular" /> 
+0

ya我已經做了這個,但我想在java中創建... – Attaullah

+0

這是java!請解釋一下你的意思! –

+0

看到我更新的問題.. – Attaullah