xmlから直接膨張されたときにfill_parentが設定されているときにスクロールビュー内の線形レイアウトの一部のビューが親を満たすのはなぜですか同じxmlを膨張させるカスタムコンポーネントを介して?アンドロイド:同じxmlは膨らませた場所によって違った働きをしますなぜか
2つの異なる場所に同じXMLを膨張の結果2つの
先頭の項目はの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>
は、私が正確にわからない正直に言うと私が作った間違いは誰かがそれを明確にすることができれば私は非常に感謝するだろう。
私はちょうど今を試みたが、私は運がなかったです。すべてが同じように見えます。 "... null)"と "新しいLayoutPar ..."の間にカンマがあるはずです。 – TomOctober
'CommunityTaskListItem'がその子を膨張させるコードを表示してください。 – Michael
完了。 \t私はまた、setLayoutParams(新しいLayoutParams(LayoutParams.FILL_PARENT、LayoutParams.WRAP_CONTENT))を呼び出すことがわかりました。 CommunityTaskListItemのいずれかが助けになりませんでした。 – TomOctober