2012-04-22 15 views
4

main.xmlを添付した簡単なアクティビティがあります。 main.xmlにはカスタムレイアウト(customLinearLayout)と単純なTextViewがあります。カスタムレイアウトには、単純なボタン付きの独自のレイアウトファイル(linearlayout.xml)もあります。レイアウトは、クラスによって正しく膨張されます。膨らんだレイアウトからビューへのアクセス方法を教えてください。

linearlayout.xmlのbuttonは、main.xmlにあるtextViewのテキストを変更しますが、そのtextViewにアクセスすることはできません。私は間違って何をしていますか?私はmain.xmlも膨らませることができません。

ここcustomLinearLayoutです:

public class CustomLinearLayout extends LinearLayout { 

LayoutInflater mInflater; 
View ContainerView; 

Button button1; 
TextView tv; 

public CustomLinearLayout(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
    init(); 
} 

public CustomLinearLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    // TODO Auto-generated constructor stub 
    init(); 
} 

private void init() { 
    mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    ContainerView = mInflater.inflate(R.layout.linearlayout, this, true); 

    button1 = (Button) ContainerView.findViewById(R.id.button1); 

    // trying to get the TextView from main.xml 
    tv = (TextView) ContainerView.findViewById(R.id.textview1); // doesn't work 

    // these tries doesn't work either 
    //tv = (TextView) ContainerView.getRootView().findViewById(R.id.textview); 
    //tv = (TextView) this.getRootView().findViewById(R.id.textview); 
    //tv = (TextView) this.findViewById(R.id.textview); 

    button1.setOnClickListener(new Button.OnClickListener() { 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(tv != null) 
       tv.setText("works"); 
      else 
       Log.d("CustomLinearLayout", "TextView not found"); 
     } 
    }); 
} 
} 

レイアウトファイル(linearlayout.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="press" /> 

</LinearLayout> 

活動:

public class TestLayoutViewsv2Activity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 
} 

活動のmain.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<test.layoutviewsv2.CustomLinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<TextView 
    android:id="@+id/textview1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

答えて

2

findViewByIdメソッドがスコープされています。カスタムビュー内には存在しないテキストフィールドがあります。しかし、それは大丈夫です!あなたの意見は自分の小さな世界でなければなりません - 彼らは自分自身の外の世界について何かを知る必要はありません。残念ながら、これを調整するためにデザインを変更しなければなりません(おそらくアクティビティへのコールバックなど)。

+1

ありがとうございました!私の考えは間違っていたと思います。私はビューが膨張した後にmain.xmlに参加すると仮定して、私が階層に戻ることができるようにしました。私はコールバックを使用して読む必要があると思います。他のビューが変更されたことをビューに知らせる他の方法はありますか? (例えば、別のビューやクラスが自分の仕事を終えたときにボタンを最初の状態に戻すように指示してください) – user1349812

+0

@ user1349812いや、これはほとんど唯一の方法です - あなたのアクティビティはすべてのビューの状態を管理します。私が言ったように、自分の小さな世界にはその見解が存在します。それは母船に何かが起きたことを伝えることができ、母船の仕事は関連する他のすべての意見が受け取った情報に従って変わることを確かめます。 – JRaymond

+0

しかし、すべてのUIを制御する階層が1つしかないことを意味しますか?オーバーロードされたアクティビティを避けるために、UIをいくつかのViewGroupsに分割するアイデアはどうですか?どのように処理されますか? - 私が約束した最後の質問:) – user1349812

0

あなたは間違った方法でinflateメソッドを使用しています。ビューを拡張している場合、レイアウトを膨張させることはありません。 inflateメソッドは、新しいインスタンスを自分で作成する必要なく、新しいビューを初期化するために使用されます。私はあなたがしようとしているものを得ていませんが、あなたはあなたのレイアウトのいくつかを再利用するためにインクルードを使うことができます。詳細はこちらをご確認くださいhttp://developer.android.com/resources/articles/layout-tricks-merge.html

+0

はい、それは私の実際の問題のほんの単純な例でした。私は、別のビューがその状態を変更したことをビューに知らせたい(別のビューまたはクラスがそのジョブを終了した初期状態に戻るようにアクティブ化されたボタンを指示するなど)。膨らませると、私はレイアウトに置かれたビューにアクセスできると思ったので、プログラムで状態を取得することができました。 – user1349812

関連する問題