如何使元素显示在布局顶部

时间:2019-06-13 12:43:54

标签: android

我知道这个问题已经问过很多次了。但是,我试图找到的所有答案都使我失败了。我希望textview出现在约束布局的顶部,但是每次我尝试运行该应用程序时,我只会看到已连接到约束布局的Web视图已贴好。

我尝试的最后一个解决方案是将布局的高程降低到1dp,并将TextView的高程提高到2dp。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:background="@drawable/green_border_background" android:elevation="1dp">


<TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="top"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          android:elevation="2dp"
          android:id="@+id/textView2"
          tools:text="0"
          android:textSize="30sp"

/>

此刻,我不在乎textview是居中,垂直于布局还是垂直于布局。我只希望它可见。它在布局下(Z钳),因此不可见。高程是我面临的问题。

3 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
    >

    <TextView
            android:textSize="25sp"
            android:layout_marginStart="50dp"
            android:text="Title"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

此代码将TextView放在ConstraintLayout的顶部

答案 1 :(得分:0)

只需检查ConstraintLayout

阅读“调整约束偏差”部分,尝试设置

app:layout_constraintVertical_bias="0"

答案 2 :(得分:0)

如果您希望将某些View置于其他View之上,那么就可以使用FrameLayout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:text="Text that you want"
        android:textSize="30sp"

        />
</FrameLayout>

FrameLayout的简单规则是,它将视图彼此堆叠,如果您希望WebView成为背景中的View,则将其作为第一个FrameLayout的子级。

现在,由于TextViewFrameLayout的最后一个子代,因此它成为FrameLayout中最高(在海拔高度上)。

编辑:

就像您说的那样,您只想要ConstraintLayout,我只用FrameLayout从上方更改了ConstraintLayout,它使TextView位于{{1}的顶部}。

WebView

好像<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:layout_width="match_parent" android:layout_height="match_parent"/> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="top" android:textSize="30sp" android:textColor="@color/white" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="Text" /> </androidx.constraintlayout.widget.ConstraintLayout> 的视图也像ConstraintLayout那样堆叠,因为当我在FrameLayout下方添加另一个WebView时,它使TextView位于顶部的WebView

相关问题