Android:ScrollView 一直包装内容而不是使用 layout_height
Android: ScrollView keeps wrapping the content instead of using layout_height
我的 ScrollView 一直将内容包裹在里面,而不是使用我给它指定的高度。
在eclipse中预览ScrollView是这样的,正是我想要的:
但是在膨胀 ScrollView 并使用 addView() 将其添加到 RelativeLayout 之后,它包装了内容,使其看起来像这样:
将 layout_height 设置为 50dp 之类的小值无效。
这是用于将 ScrollView 添加到我的 RelativeLayout 的代码:
View child = getLayoutInflater().inflate(R.layout.user_profile_template, null);
overall_layout.addView(child);
这是我正在膨胀的 ScrollView 的 xml。
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/user_profile_popup"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:background="@drawable/profile_popup_background"
android:paddingBottom="30dp"
android:visibility="visible">
<RelativeLayout
android:id="@+id/user_profile_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<Button
android:id="@+id/user_profile_add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:text="Add User"
android:visibility="gone"/>
<TextView
android:id="@+id/user_profile_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/user_profile_add_button"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:text="Username" />
<ImageView
android:id="@+id/user_profile_pic"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/user_profile_username"
android:layout_marginTop="9dp"
android:layout_marginBottom="9dp">
</ImageView>
<TextView
android:id="@+id/user_profile_about_me_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:layout_below="@id/user_profile_pic"
android:layout_centerHorizontal="true"
android:text="About Me" />
<TextView
android:id="@+id/user_profile_about_me_contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/user_profile_about_me_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:textSize="15sp"
android:textColor="#000000"
android:text="Contents of about me section
Contents of about me section
Contents of about me section
Contents of about me section
Contents of about me section
Contents of about me section
Contents of about me section
Contents of about me section" />
<ProgressBar
android:id="@+id/user_profile_about_me_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/user_profile_about_me_title" />
</RelativeLayout>
</ScrollView>
在 ScrollView 上设置 layout_marginBottom
应该可以满足您的需要。
编辑:糟糕,我没有注意到您以编程方式将滚动视图作为子项添加到 overall_layout。
按以下方式设置 overall_layout 的高度:
overall_layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, [your_height]))
我认为这是因为您在膨胀 ScrollView 时没有将父 RelativeLayout 作为根传递,所以它的宽度和高度没有正确测量。尝试以下更改:
View child = getLayoutInflater().inflate(R.layout.user_profile_template, null);
overall_layout.addView(child);
至:
View child = getLayoutInflater().inflate(R.layout.user_profile_template, overall_layout);
我的 ScrollView 一直将内容包裹在里面,而不是使用我给它指定的高度。
在eclipse中预览ScrollView是这样的,正是我想要的:
但是在膨胀 ScrollView 并使用 addView() 将其添加到 RelativeLayout 之后,它包装了内容,使其看起来像这样:
将 layout_height 设置为 50dp 之类的小值无效。
这是用于将 ScrollView 添加到我的 RelativeLayout 的代码:
View child = getLayoutInflater().inflate(R.layout.user_profile_template, null);
overall_layout.addView(child);
这是我正在膨胀的 ScrollView 的 xml。
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/user_profile_popup"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:background="@drawable/profile_popup_background"
android:paddingBottom="30dp"
android:visibility="visible">
<RelativeLayout
android:id="@+id/user_profile_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<Button
android:id="@+id/user_profile_add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:text="Add User"
android:visibility="gone"/>
<TextView
android:id="@+id/user_profile_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/user_profile_add_button"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:text="Username" />
<ImageView
android:id="@+id/user_profile_pic"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/user_profile_username"
android:layout_marginTop="9dp"
android:layout_marginBottom="9dp">
</ImageView>
<TextView
android:id="@+id/user_profile_about_me_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:layout_below="@id/user_profile_pic"
android:layout_centerHorizontal="true"
android:text="About Me" />
<TextView
android:id="@+id/user_profile_about_me_contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/user_profile_about_me_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:textSize="15sp"
android:textColor="#000000"
android:text="Contents of about me section
Contents of about me section
Contents of about me section
Contents of about me section
Contents of about me section
Contents of about me section
Contents of about me section
Contents of about me section" />
<ProgressBar
android:id="@+id/user_profile_about_me_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/user_profile_about_me_title" />
</RelativeLayout>
</ScrollView>
在 ScrollView 上设置 layout_marginBottom
应该可以满足您的需要。
编辑:糟糕,我没有注意到您以编程方式将滚动视图作为子项添加到 overall_layout。
按以下方式设置 overall_layout 的高度:
overall_layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, [your_height]))
我认为这是因为您在膨胀 ScrollView 时没有将父 RelativeLayout 作为根传递,所以它的宽度和高度没有正确测量。尝试以下更改:
View child = getLayoutInflater().inflate(R.layout.user_profile_template, null);
overall_layout.addView(child);
至:
View child = getLayoutInflater().inflate(R.layout.user_profile_template, overall_layout);