繁体   English   中英

向 textview 添加填充并添加滚动视图

[英]adding padding to textview and adding 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>

尝试使用带margin padding来达到预期的效果。 并且您应该使用LinearLayout而不是您的xmlRelativeLayout

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>

输出:

在此处输入图片说明

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM