我所做的是我以編程方式添加了一堆TextViews
,但現在ScrollView
將不會調整大小以適應在onCreate()
方法中創建的所有新的TextViews
。ScrollView不包圍整個RelativeLayout
的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>
有沒有什麼辦法讓RelativeLayout
內ScrollView
適合一切嗎?
我想你'relativelayout'的高度是'wrap_content'將使用這需要所有的總空間空間textview高度。使它成爲'match_parent'並檢查。 – Shvet
試過並沒有改變 – silverAndroid
你能顯示屏幕圖像嗎? – Shvet