2015-08-15 91 views
0

我所做的是我以編程方式添加了一堆TextViews,但現在ScrollView將不會調整大小以適應在onCreate()方法中創建的所有新的TextViewsScrollView不包圍整個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> 

有沒有什麼辦法讓RelativeLayoutScrollView適合一切嗎?

+0

我想你'relativelayout'的高度是'wrap_content'將使用這需要所有的總空間空間textview高度。使它成爲'match_parent'並檢查。 – Shvet

+0

試過並沒有改變 – silverAndroid

+0

你能顯示屏幕圖像嗎? – Shvet

回答

0

試試這個,

<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); 
} 
+0

沒有工作.... – silverAndroid

+0

嘗試通過XML而不是代碼將文本視圖添加到您的RelativeLayout。如果它以這種方式滾動,那麼代碼有問題。 –

+0

我試過了,它工作,所以我不確定我的代碼 – silverAndroid

相關問題