Я хочу установить параметры для 2 TextView в моей программе Android программно, но у меня проблема. Это интересующий кусок кода:
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);
}
Теперь проблема в том, цвет текста установлен, а гравитация - нет.
Как я могу это исправить?
Текстовые киоски с соответствующим текстом должны отображаться справа, а не под текстом Питера слева от упражнения.
Это мой 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();
}
}
Только сообщение идет вправо, но оно не идет вправо от экрана.
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>