これは私の Home.xml レイアウト ファイルです
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rellay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/placesgradient">
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="click"
/>
**<I want to include another layout here on a click of a button dynamically on click of a button >**
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="@id/btn2"
android:text="hello this is text..see above" />
</RelativeLayout>
これは私の Home.java ファイルです
public class Home extends Fragment implements OnClickListener {
Button b1;
RelativeLayout r1;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.home, container, false);
b1 = (Button) rootView.findViewById(R.id.btn2);
b1.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View arg0) {
ViewGroup con = null;
r1 = (RelativeLayout) rootView.findViewById(R.id.rellay);
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
r1.addView(layoutInflater.inflate(R.layout.test1, con , false));
}
}
ボタンをクリックすると、test1.xml レイアウト ファイルが home.xml レイアウトの一番上に配置されます。ボタンとテキストの間にある必要があります。以前にスタックでこのコードを確認しましたが、FragmentActivity では機能しないようです。ここで 1 は、この場合のインデックスまたは 2 番目の子を示します。
rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) );
どうすれば達成できますか?ヘルプ!!