我试图以编程方式将一大堆textViews添加到已经存在的LinearLayout中,该列已经包含一些文字浏览。要动态添加的文本视图应放置在LinearLayout中已存在的文本视图的右侧,从而超出屏幕区域。向HorizontalScrollView动态添加TextViews
但是,当我动态添加文本查看时,已存在的文本查看占用的宽度减小,以适应不需要的动态添加的文本查看。有没有办法让已经存在的textviews继续占据相同的区域,动态的会被添加到屏幕之外。
一些基本代码:
XML文件
<HorizontalScrollView 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:fillViewport="true">
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mediaapp.prototypingcanvas.LaunchFragment"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:paddingLeft="5dp"
android:layout_below="@+id/toggleButton"
android:layout_marginStart="20dp"
android:id="@+id/ll">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Action"
android:textColor="@color/textColorPrimary"
android:layout_weight="0.2"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="SurveyID"
android:textColor="@color/textColorPrimary"
android:layout_weight="0.2"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="Status"
android:textColor="@color/textColorPrimary"/>
<TextView
android:layout_width="0dp"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:text="CreatedOn"
android:textColor="@color/textColorPrimary"/>
<TextView
android:layout_width="0dp"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:text="Version"
android:textColor="@color/textColorPrimary"
/>
</LinearLayout>
相应的Java代码:
ll = (LinearLayout)getView().findViewById(R.id.ll);
lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(5,0,5,0);
for (int i = 0; i <WebRepo.headerListToBeShown.length; i++) {
TextView tv = new TextView(context);
tv.setText(WebRepo.headerListToBeShown[i].toString().toUpperCase());
tv.setId(Integer.parseInt(i +"1"));
tv.setX();
tv.setLayoutParams(lparams);
// li.addView(tv);
ll.addView(tv);
使用水平列表视图。 – Ali
@Ali可否请您详细说明一下 –