ScrollView не окружает весь RelativeLayout
Что я сделал, я добавил кучу TextViews
программно, но теперь ScrollView
не будет изменять размер, чтобы соответствовать всем новым TextViews
создан в onCreate()
метод.
OnCreate ():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_LEFT);
for (int i = 1; i < 21; i++) {
TextView number = new TextView(this);
number.setText(Integer.toString(i));
Log.i("i", Integer.toString(i));
number.setLayoutParams(lp);
number.setY(i / 2.54f * getResources().getDisplayMetrics().densityDpi);
relativeLayout.addView(number);
}
}
activity_main.xml:
<ScrollView
android:id="@+id/scroll_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="@+id/relative_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"/>
</ScrollView>
Есть ли способ сделать ScrollView
поместите все внутри RelativeLayout
?
1 ответ
Попробуй это,
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</RelativeLayout>
</ScrollView>
Добавьте это в свой код,
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.relative_layout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lp);
tv1.setId(1);
rLayout.addView(tv1);
for(int i=1;i<25;i++)
{
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView tv = new Button(this);
tv.setText("Hello "+i+1);
newParams.addRule(RelativeLayout.BELOW, i);
tv.setLayoutParams(newParams);
tv.setId(2);
rLayout.addView(tv);
}