向 ScrollView 动态添加 child 个视图
Dynamically adding child views to a ScrollView
我有一个 ScrollView
,它有一个 LinearLayout
作为一个 child,它包含我将像这样滚动的所有其他视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:id="@+id/profile_fields_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/account_person_name_label" />
<EditText
android:id="@+id/person_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/account_person_emailId_label" />
<EditText
android:id="@+id/person_emailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:minWidth="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/account_person_phoneNo_label" />
<EditText
android:id="@+id/person_phoneNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:maxLength="10"
android:minWidth="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/account_person_password_label" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:minWidth="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/account_person_retypePassword_label" />
<EditText
android:id="@+id/retype_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:minWidth="150dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/learner_tutor_status_label" />
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/learner_tutor_status_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/create_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/create_account_string"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_weight="0"/>
</LinearLayout>
现在,当我尝试在其末尾的 LinearLayout
(位于 ScollView
)内添加另一个视图时,动态地,它确实被添加了,但其中一半被隐藏了在 Button
下方(请参阅 ID 为 @+id/create_account
的按钮),我无法滚动到它。这意味着它不会被有效地添加到 ScrollView
。
我动态添加的这个"other"视图如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<!--Type of Tutor-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/type_of_tutor_label"
android:textStyle="italic"/>
<com.learncity.util.MultiSpinner
android:id="@+id/type_of_tutor_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Professor, Undergraduate, Freelancer..."/>
<!--Subjects/Disciplines-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/disciplines_label"
android:textStyle="italic"
android:layout_marginTop="10dp"/>
<com.learncity.util.MultiSpinner
android:id="@+id/subjects_taught_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Physics, Maths, Computer Science..."/>
</LinearLayout>
有谁知道如何使动态添加的视图有效"scrollable"?
编辑 1:
如果有人想知道这个条件视图是如何动态添加的,这里是代码的编辑版本:
void showConditionalTutorUI() {
// Initialize the first time
if(typeOfTutorMultiSpinner == null && subjectsICanTeachMultiSpinner == null){
if(profileFieldsContainer == null){
profileFieldsContainer = (ViewGroup)rootView.findViewById(R.id.profile_fields_container);
}
if(rootTutorConditionalLayout == null){
rootTutorConditionalLayout = layoutInflater.inflate(
R.layout.layout_conditional_tutor_ui,
profileFieldsContainer,
false);
}
// Add the inflated layout to the last of the container
profileFieldsContainer.addView(rootTutorConditionalLayout, profileFieldsContainer.getChildCount());
isConditionalTutorUIVisible = true;
}
编辑 2:
好的,这是 LayoutInspector 的视图:(我在层次结构中悬停的视图(用红色边框突出显示)是没有看到的视图。)
尝试将 android:fillViewport="true"
和 android:scrollbars="vertical"
添加到您的 Scroll View
。
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="vertical"
android:layout_weight="1"/>
我有一个 ScrollView
,它有一个 LinearLayout
作为一个 child,它包含我将像这样滚动的所有其他视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:id="@+id/profile_fields_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/account_person_name_label" />
<EditText
android:id="@+id/person_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/account_person_emailId_label" />
<EditText
android:id="@+id/person_emailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:minWidth="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/account_person_phoneNo_label" />
<EditText
android:id="@+id/person_phoneNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:maxLength="10"
android:minWidth="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/account_person_password_label" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:minWidth="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/account_person_retypePassword_label" />
<EditText
android:id="@+id/retype_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:minWidth="150dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/learner_tutor_status_label" />
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/learner_tutor_status_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/create_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/create_account_string"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_weight="0"/>
</LinearLayout>
现在,当我尝试在其末尾的 LinearLayout
(位于 ScollView
)内添加另一个视图时,动态地,它确实被添加了,但其中一半被隐藏了在 Button
下方(请参阅 ID 为 @+id/create_account
的按钮),我无法滚动到它。这意味着它不会被有效地添加到 ScrollView
。
我动态添加的这个"other"视图如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<!--Type of Tutor-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/type_of_tutor_label"
android:textStyle="italic"/>
<com.learncity.util.MultiSpinner
android:id="@+id/type_of_tutor_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Professor, Undergraduate, Freelancer..."/>
<!--Subjects/Disciplines-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/disciplines_label"
android:textStyle="italic"
android:layout_marginTop="10dp"/>
<com.learncity.util.MultiSpinner
android:id="@+id/subjects_taught_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Physics, Maths, Computer Science..."/>
</LinearLayout>
有谁知道如何使动态添加的视图有效"scrollable"?
编辑 1:
如果有人想知道这个条件视图是如何动态添加的,这里是代码的编辑版本:
void showConditionalTutorUI() {
// Initialize the first time
if(typeOfTutorMultiSpinner == null && subjectsICanTeachMultiSpinner == null){
if(profileFieldsContainer == null){
profileFieldsContainer = (ViewGroup)rootView.findViewById(R.id.profile_fields_container);
}
if(rootTutorConditionalLayout == null){
rootTutorConditionalLayout = layoutInflater.inflate(
R.layout.layout_conditional_tutor_ui,
profileFieldsContainer,
false);
}
// Add the inflated layout to the last of the container
profileFieldsContainer.addView(rootTutorConditionalLayout, profileFieldsContainer.getChildCount());
isConditionalTutorUIVisible = true;
}
编辑 2:
好的,这是 LayoutInspector 的视图:(我在层次结构中悬停的视图(用红色边框突出显示)是没有看到的视图。)
尝试将 android:fillViewport="true"
和 android:scrollbars="vertical"
添加到您的 Scroll View
。
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="vertical"
android:layout_weight="1"/>