【问题标题】:How to set textView alignment with Java in Android?如何在 Android 中设置 textView 与 Java 的对齐方式?
【发布时间】:2016-07-18 10:32:01
【问题描述】:

我正在开发一个包含聊天活动的应用程序。我目前正试图通过在屏幕上为传入和传出消息提供不同的颜色和位置来使其看起来更有吸引力。我似乎无法弄清楚如何使传出消息与列表视图的右侧对齐。 if 语句的第一部分用于传出消息。提前致谢!

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

【问题讨论】:

    标签: java android textview alignment


    【解决方案1】:

    试试

    textView.setGravity(Gravity.RIGHT);
    

    【讨论】:

      【解决方案2】:

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

      textView.setGravity(Gravity.RIGHT)
      

      【讨论】:

      • 聊天都还是靠左对齐的,我在原帖上附了张图片来展示当前的情况。
      • 哦,我现在明白了。您可以为传出消息创建一个新的 arrayList 和 Listview。然后制作一个自定义适配器而不是使用 simple_list_item_1。在该适配器的 textview.xml 文件中,添加这个 android:gravity="right"
      【解决方案3】:

      我猜问题是您使用的是单个列表视图或文本视图。所以如果你给重力,所有的消息都会得到重力。尝试使用两个不同的文本视图或列表视图。也许这会很有用。

      看看这个可能有用..

      left and right alignment rows inside Listview?

      【讨论】:

        猜你喜欢
        • 2018-12-30
        • 1970-01-01
        • 2018-08-12
        • 2018-11-05
        • 1970-01-01
        • 2011-04-19
        • 1970-01-01
        • 1970-01-01
        • 2017-03-02
        相关资源
        最近更新 更多