简体   繁体   中英

How do I make a ListFragment occupy only the top half of the screen

I'm trying to make a file manager application where I list the contents of a certain directory in a ListFragment in the upper half of the screen(needless to say this list could be scrollable) and when a user taps on a certain file/folder, the meta-data associated with it should be visible in a FrameLayout placed right below the fragment, along with a thumbnail image of the file type. Here's my layout:

<?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:background="#00000000">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.4" >

<fragment
    android:id="@+id/titles"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    class="com.test.fileManager.FileManagerActivity$TitlesFragment"
/>

</ScrollView>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.6"
    android:background="#00000000"
    >
</FrameLayout>

</LinearLayout>

I used 'layout_weight' property first without the ScrollView tags, but those weight properties are just not respected by the fragment and the list extends well upto the bottom of the screen.

When I enclose the fragment in ScrollView tags(I know..not a good idea!), I only see one entry of the list at a time.

Is there anyway in which I can have the ListFragment occupy the top 40% of the screen and display a scrollable list in that 40% screen space, when necessary?

First, get rid of the ScrollView . If TitlesFragment is a ListFragment , you cannot reliably put a ListView (from the ListFragment ) in a ScrollView .

Next, set the height of the fragment to be 0px . You cannot reliably use match_content as the height of a ListView .

Then, to do things purely by percentage, also set the height of your FrameLayout to 0px , then assign your weights as you wish (eg, 40 for the fragment and 60 in the FrameLayout ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM