如何将TextView放在Android的LinearLayout的上边缘?

时间:2016-08-02 07:48:11

标签: java android xml android-layout textview

我想在Android中的TextView的上边缘设置LinearLayout

我试图实现这样的目标

enter image description here

但我得到了这个:

enter image description here

到目前为止我做了什么:

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>

我为LinearLayout

创建了一个名为 bg.xml 的背景
<?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>

我对Android&amp;我觉得这很有挑战性。所以我想知道是否有人知道如何实现这个目标

感谢您的时间!!

2 个答案:

答案 0 :(得分:1)

请检查一下

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

答案 1 :(得分:0)

我会尝试做这样的事情(使用浮动操作按钮的代码,但用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>