2017-12-12 24 views
-1

私はTextViewののの値を変更することができますカスタム属性の値を変更するにはどうすればよいですか?

textTem = (TextView) findViewById(R.id.textTem); 
    textTem.setText("ssss"); 

をしかし、私は、カスタム属性を持つカスタムコンポーネントを持っている:「アンドロイド:テキストを」簡単に属性。

私のカスタム・コンポーネント・クラス:

 public class DayItem extends RelativeLayout { 
      ... 
     } 

XMLの私のカスタムコンポーネント:

 <com.example.a28210.weathpredict.DayItem 
      android:id="@+id/days1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      item:dayweather="snow" 
      item:daymaxtem="-2°" 
      item:daymintem="-12°" 
      item:daydate="12.8" /> 

私のカスタム属性:

  <?xml version="1.0" encoding="utf-8"?> 
      <resources> 
       <declare-styleable name="DayItem"> 
        <attr name="daydate" format="string" /> 
        <attr name="dayweather" format="string" /> 
        <attr name="daymaxtem" format="string" /> 
        <attr name="daymintem" format="string" /> 
       </declare-styleable> 
      </resources> 

私の質問は:変更する方法はありませんカスタム属性の値
acustom属性の値を変更するにはどうすればよいですか?

    DayItem d=(DayItem)view.findViewById(R.id.days1); 
        d.  //????? 
+3

"acustom属性の値を変更する方法はありません" - 追加します。あなたは 'com.example.a28210.weathpredict.DayItem'を書きました。その上にメソッドを追加できます。 – CommonsWare

+0

'setText'メソッドはXML定義から自動的に作成されません –

答えて

0

ビューの値を変更するには、カスタム属性の値を変更するのではなく、カスタム属性を使用します。

カスタム属性を使用して、カスタムビューで使用するXMLから値を渡します。その後、することができます

DayItem d = (DayItem)view.findViewById(R.id.days1); 
d.setWeather("Sunny") 

・ホープ、この

public class DayItem extends RelativeLayout {     
     ... 
     void setWeather(String value){ 
     someView.text = value 
     } 
     ... 

    } 

someView.text = getString(R.styleable.DayItem_dayweather) //return "snow" 

は今、あなたは自分のカスタムビューにメソッドを追加し、ランタイムからsomeViewの値を変更したい:例えば あなたを助けます。

関連する問題