ScrollView中的片段太小

时间:2011-09-20 06:40:12

标签: android android-layout android-fragments

简短问题:我希望片段位于滚动视图中,占用所有可用空间。我怎样才能做到这一点?

详细信息:我有一个片段,我放在父布局的ScrollView中。该滚动视图占据父布局中的大部分空间。尽管如此,片段在滚动视图中看起来非常小。图为:

the fragment, or the scroll view, doesn't take the whole space

这是片段的XML:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ImagePickerGridView" android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center" android:stretchMode="columnWidth"
    android:numColumns="auto_fit" android:horizontalSpacing="5dp"
    android:verticalSpacing="5dp" android:background="@color/orange1">
</GridView>

这是父布局的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1" android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center" android:stretchMode="columnWidth"
    android:numColumns="auto_fit" android:horizontalSpacing="5dp"
    android:verticalSpacing="5dp">
    <ScrollView android:layout_width="match_parent"
        android:id="@+id/imagePickerScrollView" android:layout_weight="1.0"
        android:layout_height="match_parent" android:background="@color/orange1" android:layout_gravity="fill_vertical">
    </ScrollView>
    <LinearLayout android:id="@+id/linearLayout3"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:background="@color/blue1">
        <Button android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_weight="1.0"
            android:text="@string/ImportSelectedButton" android:id="@+id/importSelectedButton"
            android:layout_marginLeft="@dimen/standardMargin"
            android:layout_marginTop="@dimen/standardMargin"
            android:layout_marginBottom="@dimen/standardMargin" android:onClick="onImportSelected"></Button>
        <Button android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_weight="1.0"
            android:text="@string/CancelButton" android:id="@+id/cancelButton"
            android:layout_marginRight="@dimen/standardMargin"
            android:layout_marginTop="@dimen/standardMargin"
            android:layout_marginBottom="@dimen/standardMargin" android:onClick="onCancel"></Button>
    </LinearLayout>
</LinearLayout>

最后,这就是我将片段添加到活动中的方式:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_picker_1);

    Log.d(CLASS_NAME, "onCreate");

    m_multiPicSelect = getIntent().getBooleanExtra(IntentHelper.MULTI_SELECT, false);
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    Fragment fragment = new ImagePickerFragment();
    fragmentTransaction.add(R.id.imagePickerScrollView, fragment);
    fragmentTransaction.commit();
}

3 个答案:

答案 0 :(得分:0)

试试这个::

<ScrollView android:layout_width="fill_parent"
        android:id="@+id/imagePickerScrollView" android:layout_weight="1.0"
        android:layout_height="fill_parent" android:background="@color/orange1" android:layout_gravity="fill_vertical">
    </ScrollView>

答案 1 :(得分:0)

...正如@nik所说,但也添加了android:fillViewport="true"

答案 2 :(得分:0)

好的,我发现了什么问题。事实证明GridView可滚动的容器,因此无需将其放在ScrollView内。一旦我将网格视图放在线性布局中,一切正常!我仍然不知道为什么GridViewScrollView内缩小了,也许是因为首先不应该在那里:)

感谢大家的回答!