Android:ScrollView保持包装内容而不是使用layout_height

时间:2015-02-27 01:42:45

标签: android xml scrollview

我的ScrollView将内容包装在其中,而不是使用我给它的指定高度。

在eclipse中预览ScrollView看起来像这样,这就是我想要的:

enter image description here

但是在给ScrollView充气并使用addView()将它添加到RelativeLayout之后,它会将内容包装成如下所示: enter image description here

将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>

2 个答案:

答案 0 :(得分:1)

我认为这是因为当您向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);

答案 1 :(得分:0)

在ScrollView上设置layout_marginBottom可以满足您的需求。

编辑:我的不好,我没注意到你以编程方式将scrollview作为子项添加到overall_layout。 按以下方式设置overall_layout的高度: overall_layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, [your_height]))