/ /プログラム的にLinearLayoutに複数のRelativeLayoutsを(既存のxmlレイアウトで)作成するにはどうすればいいですか? - アンドロイド、アンドロイドレイアウト、アンドロイドリニアレイアウト、アンドロイド相対レイアウト

LinearLayoutに複数のRelativeLayouts(既存のxmlレイアウトを使用)をプログラムで作成するにはどうすればよいですか? - android、android-layout、android-linearlayout、android-relativelayout

プログラム的に1つのLinearLayoutに複数のRelativeLayoutを配置したい。 ListViewを使用する代わりに。私のレイアウトXMLは次のようになります。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_select_app" >

<ScrollView
android:id="@+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#80000000" >

<LinearLayout
android:id="@+id/parent_linear_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF" >
</LinearLayout>
</ScrollView>
</RelativeLayout>

そして、以下はLinearLayoutの子として追加したいRelativeLayoutです。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="280dp"
android:layout_height="40dp"
android:background="@drawable/row"
android:gravity="center" >

<TextView
android:id="@+id/name_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#333333" />

</RelativeLayout>

私はプログラム的にそれをすることができますか?前もって感謝します。

回答:

回答№1は0

次のようにしてください:

public void testgenerate() {

LinearLayout rootLaout=findViewById(R.id.idofliner);
LayoutInflater layoutInflater = getLayoutInflater();

for(int i=0;i<10;i++){
RelativeLayout inflate = (RelativeLayout) layoutInflater.inflate(R.layout.relativelayutid, null);
rootLaout.addView(inflate);
}
}

回答№2の場合は0

通常RelativeLayoutをネストする必要はありません。LinearLayoutでは、1つのRelativeLayoutを使用するだけでLinearlayoutと同じ効果を得ることができます(したがって、ビュー階層で1つのレイアウトを移動する必要がなくなり、パフォーマンスが向上します)。余分なRelativeLayoutの代わりに

私はあなたが何をしたいのかわからないが、私にはそれはそのRecyclerViewまたはListViewがあなたが探しているものであるように聞こえますか?