首页 > 解决方案 > 将 TextView 隐藏在另一个视图下方的问题

问题描述

我正在制作聊天应用程序,但我不知道如何将文本隐藏在其他视图下方,这是照片:手机截图

这是我的 XML 代码:

               <?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"
android:background="@color/darker_sky_blue"
android:theme="@style/MyTheme.StatusBarColor.darker_sky_blue"
android:screenOrientation="portrait">

<ScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_gravity="bottom"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="12dp"
    android:layout_marginBottom="16dp"
    android:background="@drawable/background"
    app:layout_constraintBottom_toTopOf="@+id/editTextTextPersonName2"
    app:layout_constraintTop_toBottomOf="@+id/name">

    <LinearLayout
        android:id="@+id/chatMessages"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


    </LinearLayout>

</ScrollView>

我正在使用以下代码以编程方式添加文本:

                          LinearLayout linearLayout = findViewById(R.id.chatMessages);

                                    TextView textView = new TextView(getApplicationContext());

                                    textView.setText(oneLine);

                                    textView.setTextSize(25);

                                    textView.setTextColor(Color.parseColor("#FFFFFFFF"));

                                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                    params.setMargins(10,0,10,0);
                                    textView.setLayoutParams(params);

                                    linearLayout.addView(textView);

那么,我怎样才能得到我想要的结果呢?

标签: javaandroidandroid-layoutandroid-xml

解决方案


添加CardViewScrollView. 将背景颜色设置为卡片使用app:cardBackgroundColor并将角设置为卡片使用app:cardCornerRadius

试试这个代码:

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:cardBackgroundColor="#7CCAEA"
    app:cardCornerRadius="10dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="12dp"
    android:layout_marginBottom="16dp"
    app:cardElevation="0dp"
    app:cardMaxElevation="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/editTextTextPersonName2"
    app:layout_constraintTop_toBottomOf="@+id/name">
    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom">

        <LinearLayout
            android:id="@+id/chatMessages"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


        </LinearLayout>

    </ScrollView>
</androidx.cardview.widget.CardView>

推荐阅读