开发者社区> 问答> 正文

向textview添加填充并添加scrollview

拍照手机

所以我想知道如何在文本和气泡之间创建一个空间。我尝试了填充,但是不知道怎么做。

由于某种原因,填充不会显示在视图上。

此外,我想知道如何使聊天成为滚动视图,以便使聊天可以一直向下进行。

这就是我当时添加文本的方式:

protected void create(Context context, View v, EditText _writetexthere, int margin) {
        String inputmessage = _writetexthere.getText().toString().trim();
        RelativeLayout relativeLayout = v.findViewById(R.id.rel);
        TextView message = new TextView(context);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        //margin bubble
        lp.setMargins(700,margin,5,5);

        message.setLayoutParams(lp);
        message.setText(inputmessage);

        //text size
        message.setTextSize(15);
        message.setBackgroundResource(R.drawable.messagemine);
        message.setTextColor(Color.parseColor("#FFFFFF"));
        relativeLayout.addView(message);
    }

谢谢,

问候劳伦兹

我的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/rel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/chatwall"
        android:orientation="vertical">

        <TextView
            android:id="@+id/phonenumberchat"
            android:layout_width="match_parent"
            android:layout_height="42dp"
            android:background="@android:color/white"
            android:gravity="center"
            android:textColor="@android:color/black"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="69dp"
            android:layout_height="47dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="70dp"
            android:layout_marginEnd="15dp"
            android:layout_marginBottom="5dp"
            android:layout_weight="1"
            android:background="@drawable/sendbutton"
            android:onClick="sendmessage"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:srcCompat="@android:drawable/ic_menu_send" />

        <EditText
            android:id="@+id/writeTextHere"
            android:layout_width="290dp"
            android:layout_height="47dp"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="10dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/editextround"
            android:hint="Write Text here"
            android:inputType="textPersonName"
            android:paddingLeft="15dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="708dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="57dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" />
        </ScrollView>
    </RelativeLayout>

展开
收起
几许相思几点泪 2019-12-29 19:24:34 1081 0
1 条回答
写回答
取消 提交回答
  • 尝试使用padding与margin以达到期望的结果。而且您应该使用LinearLayout而不是RelativeLayout从xml。

    protected void create(Context context, View v, EditText _writetexthere, int margin) {
        String inputmessage = _writetexthere.getText().toString().trim();
        LinearLayout linearLayout = v.findViewById(R.id.message_layout);
        TextView message = new TextView(context);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    
        //Gravity to align right
        lp.gravity = Gravity.RIGHT;
        lp.setMargins(0, margin, 0, margin);
    
        //padding will give space for bubble
        message.setPadding(50, margin, 50, margin);
    
        message.setLayoutParams(lp);
        message.setText(inputmessage);
    
        //text size
        message.setTextSize(15);
        message.setBackgroundResource(R.drawable.test_enable);
        message.setTextColor(Color.parseColor("#FFFFFF"));
        linearLayout.addView(message);
    }
    
    

    并在ScrolView下面更改您的喜欢以滚动显示消息

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/phonenumberchat"
        android:layout_above="@+id/writeTextHere">
    
        <LinearLayout
            android:id="@+id/message_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>
    
    2019-12-29 19:25:19
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载