java - 在android中显示布局1在左边,布局2在右边

标签 java android

在android中,我试图根据消息的来源来更改布局。 如果消息来 self ,则在右侧显示布局 mymessage.xml,否则在左侧显示 message.xml。 我使用了 if 条件,但我不知道如何在右侧显示一个布局,在左侧显示第二个布局,

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // TODO
    View messageView = null;

    // Get a reference to the LayoutInflater. This helps construct the
    // view from the layout file
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    // Change the layout based on who the message is from
    if (messages.get(position).fromMe()) {


        messageView = inflater.inflate(R.layout.mymessage , parent, false);

        //initialization of 2 textView, message and time
        TextView timeView = (TextView) messageView.findViewById(R.id.mytimeTextView);
        timeView.setText(messages.get(position).getTime());

        TextView msgView = (TextView) messageView.findViewById(R.id.mymessageTextView);
        msgView.setText(messages.get(position).getMessage());
    } else {


        messageView = inflater.inflate(R.layout.message , parent, true);

        //initialization of 2 textView, message and time
        TextView timeView = (TextView) messageView.findViewById(R.id.timeTextView);
        timeView.setText(messages.get(position).getTime());

        TextView msgView = (TextView) messageView.findViewById(R.id.messageTextView);
        msgView.setText(messages.get(position).getMessage());

    }

    return messageView;
    }

最佳答案

尝试使用 mymessage.xml 的布局

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

<TextView
    android:id="@+id/mytimeTextView"
    android:alpha="0.5"
    android:layout_alignParentRight="true"
    android:text="5:00 pm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView
    android:layout_below="@+id/mytimeTextView"
    android:id="@+id/mymessageTextView"
    android:layout_alignParentRight="true"
    android:text="mymessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

message.xml 也是如此

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

<TextView
    android:id="@+id/timeTextView"
    android:alpha="0.5"
    android:layout_alignParentLeft="true"
    android:text="5:00 pm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView
    android:layout_below="@+id/timeTextView"
    android:id="@+id/messageTextView"
    android:layout_alignParentLeft="true"
    android:text="yourmessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

关于java - 在android中显示布局1在左边,布局2在右边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49318756/

相关文章:

java - Cassandra 是否有可能返回不一致的值?

java - NoSuchBeanDefinition异常 : No qualifying bean of type [Repository] found for dependency: expected at least 1 bean which qualifies as autowire

java - IBM 堆分析器 - 最终确定方法

android - 在检测到仅子节点值发生变化时访问父对象

Android,在 RecyclerView 的每个项目下显示一个阴影(没有边距填充)

java - Android - 构建 list 中出现神秘权限

android - Material Design Elevation - 如何在纯黑色背景上做到这一点

java - 根据其内容访问基于 td 元素的表中的行

android - 在 Android 上从 Webview 创建 PDF

Java - 读入json文件,解析它并写入数据库