2013-05-16 20 views
19

カスタムコンポーネントを作成して、TextViewsをほとんど入れないようにしました。コンポーネントのJavaコードからカスタムディメンション属性を読み取る方法

<resources> 
    <declare-styleable name="BasicGauge"> 
     <attr name="valueTextSize" format="dimension" /> 
     <attr name="titleTextSize" format="dimension" /> 
     <attr name="unitsTextSize" format="dimension" /> 
    </declare-styleable> 
</resources> 

サンプルの初期化:

<pl.com.digita.BikeComputerUi.customviews.BasicGauge 
    android:id="@+id/basicGauge1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:padding="10dp" 
    valueTextSize="40sp"> 

</pl.com.digita.BikeComputerUi.customviews.BasicGauge> 
今、私はテレビの

マイ属性定義のごとに独立してテキストサイズを渡し、コードから直接私のカスタムコントロールを初期化することができるようにしたいです

コンポーネントのコンストラクタでこれらの属性を読み取る方法:

final int N = typedArray.getIndexCount(); 
for (int i = 0; i < N; i++) { 
    int attribute = typedArray.getIndex(i); 
    switch (attribute) { 
     case R.styleable.BasicGauge_valueTextSize: 
      valueTextSize = typedArray.getString(attribute); 
      break; 
     case R.styleable.BasicGauge_titleTextSize: 
      titleTextSize = typedArray.getString(attribute); 
      break; 
     case R.styleable.BasicGauge_unitsTextSize: 
      unitsTextSize = typedArray.getString(attribute); 
      break; 
    } 
    typedArray.recycle(); 
} 

問題: 作成後、私のすべての値はまだnullです。 40spは私の希望する値です。

答えて

46

言うべきいくつかのこと:

  • まずあなたは正確にあなたがアンドロイドのxmlnsで行うと同じように、あなたのXMLの一番上にあるXML名前空間宣言行が必要:のxmlns:FOO = "HTTP ://schemas.android.com/apk/res-auto」
  • は、あなたがあなたのxmlnsでvalueTextSizeの前に付ける必要があります:FOO:valueTextSize = "" 40spをその後

は、それは非常に良いアイデアではありません文字列を取得するために、アンドロイドは次元を扱うためのより強力なソリューションを提供します:

int unitsTextSize = typedArray.getDimensionPixelSize(R.styleable.BasicGauge_unitsTextSize, textSize); 

は、その後、いくつかのsubtilitiesがあります

  • 塗料、またはtextpaintのために、あるとして、あなたは、この値は、することができます:paint.setTextSize(unitTextSize):
  • TextViewのために、上記のアプローチは失敗し、あなたが持っています正しい結果を得るためのsetTextのオーバーロードを使用するには:textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, unitTextSize);差は「生のピクセル」と呼ばれるものから来て

は(単なる生、濃度に応じてスケーリングされていない)と「スケーリングされたパイxels "(その反対)。受け入れ答えに少しより多くのコンテキストについては

+0

まずは、私の問題を解決していただきありがとうございます。複合コンポーネントの内部でTextViewに寸法値を渡す最善の方法は何ですか? – piotrpo

+0

あなたのやり方:カスタム属性を使用して、複合ビューの作成中にカスタム属性を取得し、作成または拡張時にビューに渡します。 – Snicolas

11

attrs.xml

dimension形式のカスタム属性を設定します。

<resources> 
    <declare-styleable name="CustomView"> 
     <attr name="valueTextSize" format="dimension" /> 
     <attr name="titleTextSize" format="dimension" /> 
     <attr name="unitsTextSize" format="dimension" /> 
    </declare-styleable> 
</resources> 

activity.xml

リファレンスあなたの属性xmlns:app=...app:...であなたのxmlでそれらを設定しています。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <com.example.myproject.CustomView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:valueTextSize="40sp" 
     app:titleTextSize="20sp" 
     app:unitsTextSize="24sp" /> 

</RelativeLayout> 

のCustomView。java

getDimensionPixelSizeを使用してTypedArrayから値を取得します。カスタムビュー内では、ピクセルで作業してください。別の次元に変換する必要がある場合は、this answerを参照してください。

public class CustomView extends View { 

    private float mValueTextSize; // pixels 
    private float mTitleTextSize; // pixels 
    private float mUnitsTextSize; // pixels 

    public CustomView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     TypedArray a = context.getTheme().obtainStyledAttributes(
       attrs, R.styleable.CustomView, 0, 0); 
     try { 
      mValueTextSize = a.getDimensionPixelSize(R.styleable.CustomView_valueTextSize, 0); 
      mTitleTextSize = a.getDimensionPixelSize(R.styleable.CustomView_titleTextSize, 0); 
      mUnitsTextSize = a.getDimensionPixelSize(R.styleable.CustomView_unitsTextSize, 0); 
     } finally { 
      a.recycle(); 
     } 
    } 

    // ... 
} 
+0

素敵できれいです。 –

関連する問題