java - 将 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);
那么,我怎样才能得到我想要的结果呢?
解决方案
添加CardView
为ScrollView
. 将背景颜色设置为卡片使用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>
推荐阅读
- reactjs - ReactNative - 在 FBSDK 中呈现 userInfo 时使用数组错误呈现一组子项?
- python - 您如何确定扩展将使用哪些版本的 VS Code?
- java - 在 java 中指定 Big-O 表示法(算法)
- ionic-framework - 字体家族占位符离子输入 Ionic5
- java - 如何使用 mod 文件 (.mcpack) 将 android 意图发送到 MinecraftPE?
- ckeditor5 - 以 2 行显示工具栏 CKEditor5 Angular
- c# - 我无法在 C# 中反序列化为 JSON。我的课有问题吗?
- flutter - 在 Dart 中实例化一个泛型类
- sql-server - 如何在 Transact SQL (SQL Server) 中获取每个员工的缺失课程?
- optimization - 如何使用 lpsolve 获得多个 BFS