フレームレイアウト内にある線形レイアウト内に2つのTextViewをプログラムで追加しようとしています。 XMLコードは次のとおりです。フレームレイアウト内の線形レイアウトにビューを動的に追加できません
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_dynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="20dp"
android:orientation="vertical" >
</LinearLayout>
<com.anheuserbusch.hifive.util.TextViewFiftySeven
android:id="@+id/txt_no_badges"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:textColor="@color/app_default_font_color"
android:text="@string/no_badges_yet"/>
</FrameLayout>
ll_dynamicは、私はプログラム的ビューを追加していた中でリニアレイアウトです。
コードは:
for (int i = 0; i < 10; i++)
{
TextViewFiftySeven txtIncentiveName = new TextViewFiftySeven(context);
TextViewFiftySeven txtDate = new TextViewFiftySeven(context);
//for Incentive name
LinearLayout.LayoutParams llptextname = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llptextname.setMargins(0, 10, 0, 0); // llp.setMargins(left, top, right, bottom);
//for date
LinearLayout.LayoutParams llptextdate = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llptextdate.setMargins(0, 1, 0, 0); // llp.setMargins(left, top, right, bottom);
txtIncentiveName.setLayoutParams(llptextname);
txtIncentiveName.setTextColor(ContextCompat.getColor(context, R.color.app_default_font_color));
txtIncentiveName.setText("Summer Incentive");
txtDate.setLayoutParams(llptextdate);
txtDate.setTextColor(ContextCompat.getColor(context, R.color.app_default_font_color));
txtDate.setText("23.2.2015");
holder.ll_dynamic.addView(txtIncentiveName);
holder.ll_dynamic.addView(txtDate);
System.out.println("Looping-->"+i);
if (i != badgeCount - 1) {
LinearLayout.LayoutParams llhorizontalLine = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 2);
llhorizontalLine.setMargins(0, 20, 0, 0); // llp.setMargins(left, top, right, bottom);
View horizontalLine = new View(context);
horizontalLine.setLayoutParams(llhorizontalLine);
horizontalLine.setBackgroundColor(ContextCompat.getColor(context, R.color.gray_light));
holder.ll_dynamic.addView(horizontalLine);
}
}
問題があり、ビューは、プログラムで追加取得されていません。しかし、フレームレイアウトを削除すると、ビューは必要に応じてレンダリングされます。明らかに、それはフレームレイアウトのために問題である。しかし、私はフレームレイアウトを維持する必要があります。
フレームレイアウト内にある場合、ビューはなぜ線形レイアウトに動的に追加されないのですか?
フレームレイアウト内に存在する線形レイアウト内のビューを追加しています...フレームレイアウト自体ではありません – kgandroid
はい、わかります。それでもレイアウト重力が働くかもしれません。または、TextViewFiftySevenをxmlから削除して、LinearLayoutが意図したとおりに動作するかどうかを確認してください。そうでない場合は、FrameLayoutを使用する必要がありますか?あなたの望む結果をもたらすかもしれない他の多くのレイアウトがあります。 – miva2
はい私はフレームレイアウトを削除することで自分の仕事を達成しました...しかし、論理的には、線形レイアウトに追加する間に間違いを見つけることができませんでした。それはフレームレイアウト内にあります。 – kgandroid