以编程方式将RelativeLayout的toRightOf更改为toLeftOf [英] Programmatically change RelativeLayout toRightOf to toLeftOf

查看:63
本文介绍了以编程方式将RelativeLayout的toRightOf更改为toLeftOf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作聊天应用程序.我想在聊天气泡之外添加时间戳.消息将左对齐(接收),右对齐(发送).

i'm making chat app. I want to put timestamp besides the chat bubble. The messages will be aligned left (for received) and right (for sent).

因此,我正在寻找一种将toLeftOf更改为toRightOf的方法.现在,它看起来像这样:

So, I'm looking for a way to change toLeftOf to toRightOf. For now, it looks like this:

这是每封邮件的XML代码(我有ArrayAdapter负责处理此问题)

Here's my XML code for each message (I have ArrayAdapter that handle this)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/bubble"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/timeStamp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/bubble" />

</RelativeLayout>

我四处搜寻并发现了这个,但没有用:

I googling around and found this, but doesn't work:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if(isReceivedMessage){
    params.addRule(RelativeLayout.RIGHT_OF, R.id.bubble);
}
else{
    params.addRule(RelativeLayout.LEFT_OF, R.id.bubble);
}
timeStampTextView.setLayoutParams(params);

有解决方案吗?谢谢

推荐答案

重写
从当前的TextView中删除属性layout_toRightOf,然后创建两个不同的LayoutParams:一个用于toRightOf,另一个用于toLeftOf.您可以根据需要切换参数.

Rewrite
Remove the attribute layout_toRightOf from the current TextView, then create two different LayoutParams: one for toRightOf and one for toLeftOf. You can switch the parameters depending on your needs.

一种替代方法是使用具有两种布局的自定义适配器.这种方法应该更快,因为您无需在运行时重新安排布局,而是在视图回收器中维护答复"和发送"布局的两个单独列表.

An alternative is to use a custom adapter with two layouts. This approach should be faster since you aren't re-arranging the layout at run-time but maintaining two separate lists of "Reply" and "Send" layouts in the view recycler.

(请注意,API 17引入了 removeRule() ,但这暂时还无济于事.)

(As a note, API 17 introduced removeRule() but this won't help just yet.)

这篇关于以编程方式将RelativeLayout的toRightOf更改为toLeftOf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆