简体   繁体   English

如何在Android中设置TextView与Java的对齐方式?

[英]How to set textView alignment with Java in Android?

I'm working on an app that incorporates a chat activity. 我正在开发一个包含聊天活动的应用程序。 I'm currently trying to make it look appealing by giving incoming and outgoing messages different colors and locations on the screen. 我目前正在尝试通过在屏幕上为传入和传出的消息提供不同的颜色和位置来使其看起来更具吸引力。 I can't seem to figure out how to make the outgoing messages align to the right of the listview. 我似乎无法弄清楚如何使外发消息与listview的右侧对齐。 The first part of the if statement is meant for the outgoing message. if语句的第一部分用于传出消息。 Thanks in advance! 提前致谢!

package com.example.muhryn.resonatem;


import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;

public class ChatList extends ArrayAdapter<String> {

    public ChatList (Activity context, List<String> values) {
        super(context,android.R.layout.simple_list_item_1,values);
    }

    public View getView (int position, View view, ViewGroup parent) {

        TextView textView=new TextView(super.getContext());

        String text=super.getItem(position);

        if (text.startsWith("(")) {
            text=text.substring(text.indexOf(')')+1).trim();
            textView.setBackgroundColor(android.graphics.Color.argb(255,251,175,66));
            textView.setTextColor(android.graphics.Color.argb(255, 26, 26, 26));
            textView.setTextSize(20);



        } else
            textView.setBackgroundColor(android.graphics.Color.argb(255,244,245,246));
            textView.setTextColor(android.graphics.Color.argb(255, 26, 26, 26));
            textView.setTextSize(20);



        textView.setText(text);

        return textView;

    }

}

How it looks like now 现在看起来如何

尝试

textView.setGravity(Gravity.RIGHT);

您可以使用它来将文本对齐到右侧

textView.setGravity(Gravity.RIGHT)

I guess the problem is you are using a single list view or text view. 我想问题是您正在使用单个列表视图或文本视图。 So if you give gravity all the messages would get the gravity. 因此,如果您赋予重力,则所有消息都将获得重力。 try using two different text views or list views. 尝试使用两个不同的文本视图或列表视图。 maybe that would be useful. 也许那会很有用。

Check this out might be useful.. 签出可能有用。

left and right alignment rows inside Listview? 列表视图中的左对齐和右对齐行?

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

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