传出消息的反向自定义布局

时间:2015-12-10 17:40:20

标签: android android-layout listview android-studio

我在右边有一个列表视图:

enter image description here

此列表视图用于消息传递。我想知道如何扭转发件人信息的布局。

更详细。 我需要发件人的邮件:

IMAGEVIEW与屏幕权利保持一致。

与IMAGEVIEW的右边对齐。

与IMAGEVIEW的权利保持一致并留下权利的信息。

时间与每个项目的布局左右对齐。(未显示在图像中)

基本上我需要现有布局来反转发送者消息。 我有代码,但目前不在我的电脑前。 如果需要代码,我很乐意添加它。 我使用基本的BaseAdapter来加载listview。

ADDED

这是自定义行布局:

int kNumPeriodsPerMove = ([&] () {
    // your code here
    return num_periods_per_move;
})();

这是我的BaseAdapter类:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="horizontal"
        android:paddingTop="10dp"
        android:weightSum="1"
        android:id="@+id/chatView">

        <ImageView
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:id="@+id/messenger_pic"
            android:layout_marginLeft="10dp"
            android:src="@drawable/profile" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="1">

            <TextView
                android:layout_width="220dp"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Medium Text"
                android:id="@+id/messenger_name"
                android:layout_marginLeft="10dp"
                android:textStyle="bold"
                android:textColor="#000000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="118dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Small Text"
                android:id="@+id/messenger_message"
                android:layout_marginLeft="10dp"
                android:textStyle="normal" />

        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/messenger_time"
            android:layout_gravity="bottom"
            android:textAlignment="center"
            android:textColor="#000000" />

    </LinearLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

你有两种方法 简单的方法是 在布局中创建两个sepearte布局,一个用于发送者消息,另一个用于rcever消息,只需隐藏和显示您想要的布局。 如果它是发送者msg隐藏接收者反之亦然。

另一种方法是以编程方式设置右侧布局。

RelativeLayout.LayoutParams params =(RelativeLayout.LayoutParams)layout.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.setLayoutParams(params);

答案 1 :(得分:1)

使用以下代码添加新的chat_view_reverse.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="horizontal"
        android:paddingTop="10dp"
        android:weightSum="1"
        android:id="@+id/chatView">

        <TextView
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/messenger_time"
            android:layout_gravity="bottom"
            android:textAlignment="center"
            android:textColor="#000000" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="1">

            <TextView
                android:layout_width="220dp"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Medium Text"
                android:id="@+id/messenger_name"
                android:layout_marginLeft="10dp"
                android:textStyle="bold"
                android:textColor="#000000" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="118dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Small Text"
                android:id="@+id/messenger_message"
                android:layout_marginLeft="10dp"
                android:textStyle="normal" />

        </LinearLayout>

        <ImageView
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:id="@+id/messenger_pic"
            android:layout_marginLeft="10dp"
            android:src="@drawable/profile" />



    </LinearLayout>

</RelativeLayout>

然后用正常或反向视图的标志条件编辑你的基本适配器getView()

if(reverse_flag){
    View row = inflater.inflate(R.layout.chat_view, parent, false);
}else{
    View row = inflater.inflate(R.layout.chat_view_reverse, parent, false);
}
相关问题