我想动态地将多个horizontalscrollView添加到线性布局。为此,我有一个id为mainList的linearLayout。和一个带horizontalScrollLayout的xml如下所示。它不起作用。什么是写作方式。
mainListView = (LinearLayout) findViewById(R.id.mainList);
hrscroll = (HorizontalScrollView) findViewById(R.id.hrscroll);
// hrtxt=(TextView) findViewById(R.id.hrtxt);
for(int i=0;i<categories.length;i++) {
mainListView.addView(hrscroll);
}
horizontalScrollLayout的XML
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/hrscroll"
android:layout_height="wrap_content">
</HorizontalScrollView>
答案 0 :(得分:0)
动态地将HorizontalScrollView
添加到LinearLayout
mainListView = (LinearLayout) findViewById(R.id.mainList);
// Creating mHorizontalScrollView array for multiple Categories
HorizontalScrollView[] mHorizontalScrollView = new HorizontalScrollView[categories.length];
for(int i = 0; i < categories.length; i++) {
mHorizontalScrollView[i] = new HorizontalScrollView(context);
mainListView.addView(mHorizontalScrollView[i]);
}
更新答案:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" >
<HorizontalScrollView
android:id="@+id/categoryOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<HorizontalScrollView
android:id="@+id/categoryTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<HorizontalScrollView
android:id="@+id/categoryThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<HorizontalScrollView
android:id="@+id/categoryFour"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<HorizontalScrollView
android:id="@+id/categoryFive"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<HorizontalScrollView
android:id="@+id/categorySix"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<HorizontalScrollView
android:id="@+id/categorySeven"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
活动文件:
绑定XML视图:
mainListView = (LinearLayout) findViewById(R.id.mainList);
categoryOne = (HorizontalScrollView) findViewById(R.id.categoryOne);
categoryTwo = (HorizontalScrollView) findViewById(R.id.categoryTwo);
categoryThree = (HorizontalScrollView) findViewById(R.id.categoryThree);
categoryFour = (HorizontalScrollView) findViewById(R.id.categoryFour);
categoryFive = (HorizontalScrollView) findViewById(R.id.categoryFive);
categorySix = (HorizontalScrollView) findViewById(R.id.categorySix);
categorySeven = (HorizontalScrollView) findViewById(R.id.categorySeven);