简体   繁体   中英

How to place a TextView right on the upper edge of a LinearLayout in Android?

I want to set a TextView right on the upper edge of a LinearLayout in Android.

I'm trying to achieve something like this

在此处输入图片说明

But I'm getting this:

在此处输入图片说明

What I have done so far:

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/tableLayout"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="tries.auro.tryapp.MainActivity"
    tools:showIn="@layout/activity_main"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:scrollbars="vertical"
        android:background="@drawable/bg"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="33dp"
        android:id="@+id/linearLayout">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="BLAH BLAH!!"
            android:id="@+id/textView"
            android:layout_marginLeft="33dp"
            android:layout_marginStart="33dp"
            android:layout_alignTop="@+id/linearLayout"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
    </LinearLayout>

</RelativeLayout>

And I made a background, called bg.xml for my LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:shape="rectangle">
            <solid android:color="#000000"/>
        </shape>
    </item>

    <item
        android:left="2dp"
        android:right="2dp"
        android:bottom="2dp"
        android:top="2dp">
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF"/>
        </shape>
    </item>

</layer-list>

I am quite new to Android & I find this quite challenging. So I was wondering if anyone knew how I could achieve this

Thank you for your time!!

Please check this one

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:background="@drawable/bg" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="15dp"
    android:background="@drawable/bg"
    android:text=";dslf;ldfsgk;" />

I would try to do something like this (Use the code of floating action button but change the floating action button with a TextView):

<android.support.design.widget.CoordinatorLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout>
        <LinearLayout
            android:id="@+id/viewA"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.15">

            </LinearLayout>

        <LinearLayout
            android:id="@+id/viewB"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.85" >

        </LinearLayout>

    </LinearLayout>

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        app:layout_anchor="@id/viewA"
        app:layout_anchorGravity="left|bottom"/>

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

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