2011-09-11 9 views
0

xmlから直接膨張されたときにfill_parentが設定されているときにスクロールビュー内の線形レイアウトの一部のビューが親を満たすのはなぜですか同じxmlを膨張させるカスタムコンポーネントを介して?アンドロイド:同じxmlは膨らませた場所によって違った働きをしますなぜか

2つの異なる場所に同じXMLを膨張の結果2つの

http://imgur.com/DF6kl

先頭の項目はのLinearLayoutを拡張するクラスのXMLを膨張し、そのクラスを追加した結果でありますスクロールビュー内のlinearLayoutに設定します。このビューは親を満たしていないので、なぜ私はうまくいかない。

下の項目は、これがすべて含まれているアクティビティでxmlを直接膨張させた結果です。それは私が期待したように現れます。

View communityTask = View.inflate(context, R.layout.communitytask, null); 
addView(communityTask); 

私が想定しています。これは、カスタム・コンポーネント・クラス「CommunityTaskListItem」内のアクティビティを膨張させるコードがある

scrollList.addView(new CommunityTaskListItem(this, null)); 
scrollList.addView(View.inflate(getBaseContext(), R.layout.communitytask, null)); 

この

は、活動の二つのビューを追加するコードですxmlは大丈夫です。なぜなら、アクティビティに直接展開されたときにうまく動作するからです。私の質問は、なぜfill_parentプロパティがxmlがカスタムコンポーネントのクラスで拡張されたときに失われているように見えるのですか?

いいえ対策として、ここでは膨らませているxmlです:編集:私は前に間違ったものを与えた!

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"> 
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:background="@drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1"> 
     <TextView android:layout_height="wrap_content" style="@style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView> 
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="@style/CommunityTaskText" android:id="@+id/textViewCommunityTaskDescription" android:text="Task"></TextView> 
    </LinearLayout> 

</LinearLayout> 

私はできれば私の活動の中にXMLを膨らませるカスタムコンポーネントを維持していないしたいと思います。ここでは正しいものです。

EDIT:コードCommunityTaskListItemが膨張した:

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class CommunityTaskListItem extends LinearLayout { 

    TextView textViewCommunityTaskTimes; 
    TextView textViewCommunityTaskDescription; 

    public CommunityTaskListItem(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     View communityTask = View.inflate(context, R.layout.communitytask, null); 
     addView(communityTask); 

     textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes); 
     textViewCommunityTaskDescription = (TextView)findViewById(R.id.textViewCommunityTaskDescription); 

    } 
... 

EDIT:すべての今働いて!ここでは、彼らは同様の問題を持っている包み他の誰のための最終的なコードは次のとおりです。カスタムオブジェクトのための コンストラクタ:

public CommunityTaskListItem(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     setOrientation(VERTICAL); 
     addView(inflate(context, R.layout.communitytask, null)); 

     textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes); 
     textViewCommunityTaskDescription =  (TextView)findViewById(R.id.textViewCommunityTaskDescription); 
    } 

カスタムオブジェクトのxml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:background="@drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1"> 
     <TextView android:layout_height="wrap_content" style="@style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView> 
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="@style/CommunityTaskText" android:id="@+id/textViewCommunityTaskDescription" android:text="Task"></TextView> 
    </LinearLayout> 

</LinearLayout> 

は、私が正確にわからない正直に言うと私が作った間違いは誰かがそれを明確にすることができれば私は非常に感謝するだろう。

答えて

2

最初の行は次のようになります。

scrollList.addView(new CommunityTaskListItem(this, null) 
    new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

あなたはinflate()CommunityTaskListItem内部クラスを呼び出すと、膨張したビューがCommunityTaskListItemビューの子ビューになります。しかし、このビューの幅と高さは設定しないので、親ビューの幅と一致しません。

EDITpackagenameCommunityTaskListItemクラスのパッケージの名前です

<?xml version="1.0" encoding="utf-8"?> 
<packagename.CommunityTaskListItem 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"> 
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:background="@drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1"> 
     <TextView android:layout_height="wrap_content" style="@style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView> 
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="@style/CommunityTaskText" android:id="@+id/textViewCommunityTaskDescription" android:text="Task"></TextView> 
    </LinearLayout> 

</packagename.CommunityTaskListItem> 

:私はあなたのXMLは次のようになりべきだと思います。あなたがCommunityTaskListItemを変更する必要があります。この場合

public class CommunityTaskListItem extends LinearLayout { 

    TextView textViewCommunityTaskTimes; 
    TextView textViewCommunityTaskDescription; 

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

    @Override 
    protected void onFinishInflate() { 
     textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes); 
     textViewCommunityTaskDescription = (TextView)findViewById(R.id.textViewCommunityTaskDescription); 
    } 

    // .... 
} 

このクラスは、コンストラクタを使用して作成することができません。それは膨らませることしかできません。

2番目の方法は、あなたのように子どもをコンストラクタに展開することです。しかし、XMLが異なる必要があります。

<?xml version="1.0" encoding="utf-8"?> 
<merge 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:background="@drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1"> 
     <TextView android:layout_height="wrap_content" style="@style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView> 
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="@style/CommunityTaskText" android:id="@+id/textViewCommunityTaskDescription" android:text="Task"></TextView> 
    </LinearLayout> 

</merge> 

CommunityTaskListItemは次のようになります。 パブリッククラスCommunityTaskListItemは{

TextView textViewCommunityTaskTimes; 
    TextView textViewCommunityTaskDescription; 

    public CommunityTaskListItem(Context context) { 
     this(context, null); 
    } 

    public CommunityTaskListItem(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     setOrientation(VERTICAL); 
     inflate(context, R.layout.communitytask); 

     textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes); 
     textViewCommunityTaskDescription = (TextView)findViewById(R.id.textViewCommunityTaskDescription); 
    } 

    // .... 
} 

このクラスは、そのコンストラクタを使用して作成し、XMLから膨張させることができるのLinearLayoutを拡張します。しかし、それを膨らませたい場合は、別のXMLファイルを作成する必要があります。 task.xmlとしましょう。

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

そして、それはあなたがそれを使用することができます方法は次のとおりです。

scrollList.addView(new CommunityTaskListItem(this), 
    new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
scrollList.addView(View.inflate(getBaseContext(), R.layout.task, null)); 
+0

私はちょうど今を試みたが、私は運がなかったです。すべてが同じように見えます。 "... null)"と "新しいLayoutPar ..."の間にカンマがあるはずです。 – TomOctober

+0

'CommunityTaskListItem'がその子を膨張させるコードを表示してください。 – Michael

+0

完了。 \t私はまた、setLayoutParams(新しいLayoutParams(LayoutParams.FILL_PARENT、LayoutParams.WRAP_CONTENT))を呼び出すことがわかりました。 CommunityTaskListItemのいずれかが助けになりませんでした。 – TomOctober

関連する問題