我有一個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的大小沒有按我的預期出現。這是一個的,它看起來像一個設備,這說明文字上的截圖被切出時,它遷移到第二行:
那麼,如何才能確保線性佈局重新大小,以適應兒童不同高度的意見?
當我這樣做時,它隱藏了更多的文字。 – spacitron
當您通過編程添加多個元素來完成此操作時,wrap_content會根據包裝第一個元素的內容所需的高度來設置所有元素的高度。所以這個解決方案可以工作,只要你的第一個元素永遠是你最大的。 –
等什麼?所以沒有辦法動態調整元素的大小。 – spacitron