2014-04-09 53 views
0

我有一個ScrollView,我試圖動態填充。這是一個包含了滾動的XML:Android:ScrollView在添加子視圖時不重新調整大小

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#f6f5f5" > 

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#40ff0000" 
    android:paddingBottom="70dp" 
    android:paddingTop="20dp" 
    android:paddingLeft="20dp" 
    android:paddingRight="20dp"> 

    <LinearLayout 
     android:id="@+id/option_list_container" 
     android:background="#40ffff00" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="20dp" 
      android:background="#292929" 
      android:paddingBottom="5dp" 
      android:paddingLeft="15dp" 
      android:paddingTop="5dp" 
      android:text="Stap 3/5" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="@color/white" /> 
    </LinearLayout> 
</ScrollView> 

,這裏是爲我將在ScrollvVew元素中的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

<np.custom.FontIcon 
    android:id="@+id/option_icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/calendar_circleround" 
    android:textSize="40sp" 
    android:gravity="center" 
    android:text="@string/planit_selection_empty_circle" 
    android:textColor="#292929" /> 

<TextView 
    android:id="@+id/option_text" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical" 
    android:layout_marginLeft="10dp" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 

這是我使用的代碼填充scrollview:

ArrayList<String> g = new ArrayList<String>(); 
    g.add("Fix a Bug"); 
    g.add("Buy a new PC"); 
    g.add("Make Coffee"); 
    g.add("Take a Break"); 
    g.add("Don't do that"); 
    g.add("Throw your hands in the air like you just don't care!"); 
    LinearLayout optionListLayout = (LinearLayout) rootView.findViewById(R.id.option_list_container); 
    LayoutInflater inflater = LayoutInflater.from(getActivity()); 
    for(String p:g){ 
     View v = inflater.inflate(R.layout.planit_option_item, null); 
     TextView optText = (TextView) v.findViewById(R.id.option_text); 
     optText.setText(p); 
     optionListLayout.addView(v); 
    } 

這一切都工作得很好,前認爲它包含的ScrollView和LinearLayout的大小沒有按我的預期出現。這是一個的,它看起來像一個設備,這說明文字上的截圖被切出時,它遷移到第二行:

enter image description here

那麼,如何才能確保線性佈局重新大小,以適應兒童不同高度的意見?

回答

0

你的問題不是ScrollView不適應新的孩子。問題在於,由於R.layout.planit_option_item中的佈局參數,文本在跨越多行時被截斷。嘗試更改ID @+id/option_textTextView,使其高度爲wrap_content

+0

當我這樣做時,它隱藏了更多的文字。 – spacitron

+0

當您通過編程添加多個元素來完成此操作時,wrap_content會根據包裝第一個元素的內容所需的高度來設置所有元素的高度。所以這個解決方案可以工作,只要你的第一個元素永遠是你最大的。 –

+0

等什麼?所以沒有辦法動態調整元素的大小。 – spacitron

0

更改您的TextView的滾動型元素,以適應兩行:

... 
<TextView 
    android:id="@+id/option_text" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical" 
    android:layout_marginLeft="10dp" 
    android:maxLines="2"   
    android:singleLine="false" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
... 

這不是滾動型的問題,它與你插入了滾動元件的規格的問題。如果您預計在某些情況下可能會有超過2行的文本,請設置maxLines = X,其中X是您期望的最大行數。

請讓我知道如果這不起作用,我會很樂意進一步幫助。

相關問題