简体   繁体   中英

How to position a TextView above ListView inside Linear Layout

I'm attempting to place a TextView above a ListView inside a Linear Layout but I'm getting nowhere fast.. I've tried placing the TV in it's own layout, I've tried adding a margin to the top of the LV, but nothing seems to work.. The TV appears to have some kind of unremovable padding.. ? Please see screenshot and .XML below

Design View layout

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/setting_frag"
    android:baselineAligned="false">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello"/>

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="?attr/colorBackgroundFloating"
        android:descendantFocusability="blocksDescendants" />


    <RelativeLayout
        android:id="@+id/setting_dialogue"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <ViewStub
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/device_info_stub"
            android:inflatedId="@+id/device_info_stub_inflated_id"
            android:layout="@layout/device_info"
            />
        <ViewStub
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/health_check_stub"
            android:inflatedId="@+id/health_check_stub_inflated_id"
            android:layout="@layout/health_checks"
            />
        <ViewStub
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/system_config_stub"
            android:inflatedId="@+id/system_config_stub_inflated_id"
            android:layout="@layout/system_configuration"
            />
        <ViewStub
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/database_stats_stub"
            android:inflatedId="@+id/database_stats_stub_inflated_id"
            android:layout="@layout/database_stats"
            />
        <ViewStub
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/check_update_stub"
            android:inflatedId="@+id/device_info_stub_inflated_id"
            android:layout="@layout/device_info"
            />

    </RelativeLayout>

Screenshot

New screenshot

change android:orientation="horizontal" to android:orientation="vertical"


As you want your List and relative stub side by side use following code.

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/setting_frag"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:baselineAligned="false"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:baselineAligned="false"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F00"
            android:textColor="@color/white"
            android:text="hello" />


        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#0F0"
            android:descendantFocusability="blocksDescendants" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/setting_dialogue"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00F"
        android:layout_weight="1">

        <ViewStub
            android:id="@+id/device_info_stub"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inflatedId="@+id/device_info_stub_inflated_id"
            android:layout="@layout/activity_burst_assignees" />

        <ViewStub
            android:id="@+id/health_check_stub"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inflatedId="@+id/health_check_stub_inflated_id"
            android:layout="@layout/activity_burst_assignees" />

        <ViewStub
            android:id="@+id/system_config_stub"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inflatedId="@+id/system_config_stub_inflated_id"
            android:layout="@layout/activity_burst_assignees" />

        <ViewStub
            android:id="@+id/database_stats_stub"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inflatedId="@+id/database_stats_stub_inflated_id"
            android:layout="@layout/activity_burst_assignees" />

        <ViewStub
            android:id="@+id/check_update_stub"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inflatedId="@+id/device_info_stub_inflated_id"
            android:layout="@layout/activity_burst_assignees" />

    </RelativeLayout>


</LinearLayout>

The output of the code will be like below

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weight_sum ="11"
    android:id="@+id/setting_frag"
    android:baselineAligned="false">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:text="hello"
    android:layout_weight="2"/>

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:background="?attr/colorBackgroundFloating"
        android:descendantFocusability="blocksDescendants" />


    <RelativeLayout
        android:id="@+id/setting_dialogue"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4">
        <ViewStub
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/device_info_stub"
            android:inflatedId="@+id/device_info_stub_inflated_id"
            android:layout="@layout/device_info"
            />
        <ViewStub
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/health_check_stub"
            android:inflatedId="@+id/health_check_stub_inflated_id"
            android:layout="@layout/health_checks"
            />
        <ViewStub
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/system_config_stub"
            android:inflatedId="@+id/system_config_stub_inflated_id"
            android:layout="@layout/system_configuration"
            />
        <ViewStub
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/database_stats_stub"
            android:inflatedId="@+id/database_stats_stub_inflated_id"
            android:layout="@layout/database_stats"
            />
        <ViewStub
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/check_update_stub"
            android:inflatedId="@+id/device_info_stub_inflated_id"
            android:layout="@layout/device_info"
            />

    </RelativeLayout>

I have made the changes. This will work for you.

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