android - LinearLayout 中的 TextView 参数无法以编程方式工作

标签 android android-linearlayout

我想在我的 Android 程序中以编程方式为 2 TextView 设置参数,但我遇到了问题。这是一段有趣的代码:

private void setChatItemStyle(Boolean mineMsg, ChatViewHolder holder) {

        if(mineMsg) {
            holder.params.gravity = Gravity.END;
            holder.autore.setTextColor(Color.BLUE);
        }else{
            holder.params.gravity = Gravity.START;
            holder.autore.setTextColor(Color.MAGENTA);
        }

        holder.autore.setLayoutParams(holder.params);
        holder.messaggio.setLayoutParams(holder.params);
    }

现在的问题是;设置了文本的颜色,但没有设置重力。 我该如何解决?

TextView Cioscos 及其各自的文本应显示在右侧,而不是 Activity 左侧的 Peter 文本下方。

这是我的 ChatViewHolder:

public class ChatViewHolder extends RecyclerView.ViewHolder {

        TextView autore, messaggio;
        LinearLayout.LayoutParams params;

        public ChatViewHolder(View itemView) {
            super(itemView);

            autore = itemView.findViewById(R.id.tv_autore);
            messaggio = itemView.findViewById(R.id.tv_messaggio);
            params = (LinearLayout.LayoutParams) autore.getLayoutParams();
        }
    }

Edit in app

只有消息在右边,但它仍然没有在屏幕的右边。

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="visible">

    <TextView
        android:id="@+id/tv_autore"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="Autore"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="24sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_messaggio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Messaggio" />
</LinearLayout>

最佳答案

当我们没有 TextView 的布局时很难回答,它应该在您的 ChatViewHolder 中。

关于 gravity 我能说的是,gravity 有两种类型。

  1. LayoutParams 中的重力等于 xml 中的layout_gravity(TextView 的布局)
  2. View 中的 Gravity 等于 xml 中的 gravity )

在你的情况下:

  1. 如果您的 TextView 宽度为 WRAP_CONTENT,将会工作
  2. 如果您的 TextView 的宽度为 MATCH_PARENT,将会工作

针对您的情况的一种方法:

private void setChatItemStyle(Boolean mineMsg, ChatViewHolder holder) {

    int gravity = Gravity.START;
    if (mineMsg) {
        gravity = Gravity.END;
        holder.autore.setTextColor(Color.BLUE);
    } else {
        holder.autore.setTextColor(Color.MAGENTA);
    }

    holder.autore.setGravity(gravity);
    holder.messaggio.setGravity(gravity);
}

同时在您的 xml 文件中将 TextViews 设置为 match_parent

我使用 kotlin,所以语法可能有误?对不起,如果我遗漏了什么,你明白了......

关于android - LinearLayout 中的 TextView 参数无法以编程方式工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55851483/

相关文章:

android - 我如何制作像 Skype 应用程序这样的介绍性标识动画?

android - 我可以以 root 身份启动 android intent 吗?

java - 运行另一个应用程序,如果失败打开 Google Play

android - 如何坚持彼此的观点

Android,LinearLayout 不会滚动

安卓 : Border only on corners

android - 从实时android中的视频中提取帧

android - 如何为 LinearLayout 背景图像赋予形状?

java - 如何将 "custom"选项设置为查看

java - 将 Android 屏幕一分为二