资源文件中的wrap_content行为类似于match_parent

时间:2018-07-25 06:33:05

标签: java android android-layout

enter image description here屏幕截图-https://ibb.co/mn0bZT             -https://ibb.co/fMcjM8

如您所见,它是一个recyclerview,应该是包装内容,但是有些行为有所不同...我对此没有任何解释,也许有人可以帮助我...

代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">


<android.support.design.widget.AppBarLayout
    android:id="@+id/actionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/black">

        <RelativeLayout
            android:padding="2dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

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


                <TextView
                    android:text="Name"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="5dp"
                    android:id="@+id/name"
                    android:textColor="@color/white"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="20dp"/>

                <TextView
                    android:text="Online"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="5dp"
                    android:id="@+id/online"
                    android:textColor="@color/white"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15dp"/>

            </LinearLayout>

            <ImageButton
                android:layout_centerVertical="true"
                android:background="@null"
                android:src="@drawable/search"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />


        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:layout_above="@+id/linearlayout"
    android:scrollbars="vertical"
    android:id="@+id/message_list"
    android:layout_below="@+id/actionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>


<RelativeLayout
    android:padding="1dp"
    android:layout_alignParentBottom="true"
    android:id="@+id/linearlayout"
    android:layout_width="match_parent"
    android:layout_height="50dp">


<ImageButton
    android:src="@drawable/add_image"
    android:id="@+id/add_image"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true" />

<EditText
    android:textColorHint="@color/black"
    android:hint="Write a message..."
    android:id="@+id/input_message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toEndOf="@+id/add_image"
    android:layout_toStartOf="@+id/send_message" />

    <ImageButton
        android:src="@drawable/send_button"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/send_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send" />

</RelativeLayout>

自定义

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/message_single_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp">

<TextView
    android:id="@+id/message_text_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@drawable/text_background2"
    android:padding="10dp"
    android:text="Textview"
    android:textSize="15dp"
    android:textColor="@color/black"/>

</RelativeLayout>

请有人帮我...预先感谢

im用于对齐文本视图的方法是-

if (from_user != null && from_user.equals(current_user_id)) {
        holder.messageText.setTextColor(Color.BLACK);


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

                if (Seen==1){
                    holder.messageText.setBackgroundResource(R.drawable.text_background2);
                }else{
                    holder.messageText.setBackgroundResource(R.drawable.text_background1);
                }

    }else {
            holder.messageText.setBackgroundResource(R.drawable.text_background2);
            holder.messageText.setTextColor(Color.BLACK);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.messageText.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        holder.messageText.setLayoutParams(params);
        }

        holder.messageText.setText(messages.getMessage());
    }

1 个答案:

答案 0 :(得分:0)

在添加新规则之前尝试删除规则

我们在屏幕截图中可以看到左侧完美显示了两个消息( ok和ufff )。

  

params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);   params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);

if (from_user != null && from_user.equals(current_user_id)) {
    holder.messageText.setTextColor(Color.BLACK);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.messageText.getLayoutParams();
    //remove rules
    params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);

    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    holder.messageText.setLayoutParams(params);

            if (Seen==1){
                holder.messageText.setBackgroundResource(R.drawable.text_background2);
            }else{
                holder.messageText.setBackgroundResource(R.drawable.text_background1);
            }

}else {
        holder.messageText.setBackgroundResource(R.drawable.text_background2);
        holder.messageText.setTextColor(Color.BLACK);

    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.messageText.getLayoutParams();
    //remove rules
    params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    holder.messageText.setLayoutParams(params);
    }

    holder.messageText.setText(messages.getMessage());
}