简体   繁体   English

如何在Android的LinearLayout的上边缘上放置一个TextView?

[英]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. 我想在Android的LinearLayout的上边缘设置一个TextView

I'm trying to achieve something like this 我正在努力实现这样的目标

在此处输入图片说明

But I'm getting this: 但是我得到这个:

在此处输入图片说明

What I have done so far: 到目前为止,我所做的是:

content_main.xml 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 我为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>

I am quite new to Android & I find this quite challenging. 我对Android还是很陌生,我觉得这很有挑战性。 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): 我会尝试做这样的事情(使用浮动动作按钮的代码,但使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Android 中将 TextView 添加到 LinearLayout - How to add a TextView to LinearLayout in Android Textview不粘在屏幕的右边缘(Android / Java) - Textview not sticking to the right edge of the screen (Android/Java) 如何在Android中的LinearLayout中对齐垂直textview和editText - How to align vertical textview and editText in Linearlayout in android 将TextView设置为LinearLayout(Vertical)的右侧 - Set TextView To Right Of LinearLayout(Vertical) Android TextView和ProgressBar不在LinearLayout中显示 - Android TextView and ProgressBar not showing in LinearLayout 如何从右到左放置textView的垂直滚动条? - How to place vertical scrollbar of textView from right to left? 如何在Android中使用if else条件将2个TextView放置在同一个地方 - How to place 2 TextView at same place with if else condition in Android 如何以编程方式将Android TextView对齐到另一个TextView的右侧 - How to programmatically align an Android TextView to the right of another TextView Android中LinearLayout中微调器和TextView的布局对齐 - Layout alignment of spinner and TextView in LinearLayout programmatically android Android,在方法之间传递LinearLayout,RelativeLayout和TextView - Android, passing LinearLayout, RelativeLayout and TextView between methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM