简体   繁体   中英

Unable to align TextView above Coordinator Layout

I have a Coordinator Layout showing cardview, I wanted to show TextView above this coordinator layout, I tried putting Coordinator Layout inside Linear Layout just like how we do for Frame Layout, but that didn't work. Can someone help

在此处输入图片说明

Code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:comfort="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:background="@color/transparent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="Hello World"
        android:textColor="@color/eula_body_text_color"
        android:textSize="19sp" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:tabStripEnabled="false"
        android:layout_height="wrap_content"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="@dimen/_16sdp"
        android:paddingLeft="12dp"
        android:paddingRight="-12dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


</android.support.design.widget.CoordinatorLayout>

Main Problem is you want textview outside the coordinatorlayout but you are defining as child of that layout. So, you need to create a parent layout which have two childs ie 1. TextView and 2. CoordinatorLayout.

<LinearLayout>
   <TextView/>
   <CoordinatorLayout>
     all Views
   </CoordinatorLayout>
</LinearLayout>

try this

<?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">

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="Hello World"
        android:textColor="@color/eula_body_text_color"
        android:textSize="19sp" />

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:comfort="http://schemas.android.com/tools"
        android:id="@+id/main_content"
        android:background="@color/transparent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:tabStripEnabled="false"
            android:layout_height="wrap_content"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/_16sdp"
            android:paddingLeft="12dp"
            android:paddingRight="-12dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


    </android.support.design.widget.CoordinatorLayout>
    </LinearLayout>

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