2011-08-19 18 views
5

xmlレイアウトをカスタムViewGroupに拡張しようとしています。レイアウトを拡張した後、ViewGroupは、レイアウトのルートビューのみを表示します。実際には、完全レイアウトファイルをViewGroupに表示する必要があります。カスタムViewGroupへのXMLレイアウトの膨張

私はstackoverflowや他のウェブサイトでこれに関連して掲載されている他の同様の質問を行っているが、役に立たなかった。ここで

は私のカスタム ViewGroupのコードです:レイアウトファイル

ViewEvent event = new ViewEvent(getApplicationContext()); 
LayoutInflater inflator; 
inflator=(LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
inflator.inflate(R.layout.try1, event); 
setContentView(event); 

以下の通りである:

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


    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#ffff0000" 
     android:text="asdasadfas" /> 

</LinearLayout> 
Activity

public class ViewEvent extends ViewGroup { 
private final String LOG_TAG="Month View Event"; 
public ViewEvent(Context context) { 
     super(context); 
    } 

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

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) {  

     int childCount= getChildCount(); 
     for(int i=0;i<childCount;i++) 
     { 
      Log.i(LOG_TAG, "Child: " + i + ", Layout [l,r,t,b]: " + l + "," + r + "," + t + "," + b); 
      View v=getChildAt(i); 
      if(v instanceof LinearLayout) 
      { 
       Log.i(LOG_TAG, "Event child count: " + ((ViewGroup)v).getChildCount()); // displaying the child count 1 
       v.layout(0, 0, r-l, b-t); 
      } 
      //v.layout(l, r, t, b); 
     } 
    } 

、私はこのカスタムビューグループを使用しています

このレイアウトを拡張した後、私はルートを取得していますLinearLayoutのバックグラウンドカラーは#ffff0000です。 TextViewは表示されません。

どこが間違っているのか、見つからないのですか?

答えて

-1

このようにしてみてください。あなたのTextViewのIDは宣言していません。

このTextViewには、次のようにアクセスします。

TextView testTextView = (TextView) ViewEvent.findViewById(R.id.testTextId); 
+0

それはアクセス可能ですが、それは表示されません! –

2

(あなたViewEvent)イベントのgetParent()を試してみてください。

inflator.inflate(R.layout.try1, event.getParent(), false); 
関連する問題