Android 以编程方式在后台设置布局9修补程序图像效果不佳

Android 以编程方式在后台设置布局9修补程序图像效果不佳,android,xml,android-linearlayout,nine-patch,Android,Xml,Android Linearlayout,Nine Patch,以下是xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_co

以下是xml:

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

    <TextView
        android:id="@+id/textview_chatdate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="date"
        android:layout_marginTop="@dimen/chattab_messsagelayout_marginbottom"
        android:textSize="@dimen/chattab_textview_date_fontsize"
        android:textColor="@color/chattab_textview_date_fontcolor"
        android:background="@android:color/transparent"
        />
    <LinearLayout
        android:id="@+id/llayout_chatlayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textview_chatdate"
        android:layout_marginTop="@dimen/chattab_messsagelayout_margintop"
        android:layout_marginLeft="@dimen/chattab_messsagelayout_marginleft"
        android:layout_marginRight="@dimen/chattab_messsagelayout_marginleft"
        android:layout_alignParentRight="true"
        android:background="@drawable/aqua"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textview_chatname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:padding="5dp"
            android:text="username"
            android:textSize="@dimen/chattab_edittext_message_fontsize"
            android:textColor="@color/chattab_textview_chatname_fontcolor"
            android:lines="1"
            android:maxLines="1"/>
        <TextView
            android:id="@+id/textview_chatmessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:padding="5dp"
            android:text="message here"
            android:textSize="@dimen/chattab_edittext_message_fontsize"
            android:textColor="@color/chattab_textview_date_fontcolor"
            android:maxLines="3"/>
    </LinearLayout>
</RelativeLayout>





  if(broadcastResponse.getUid().equals(uId))
                    {
             RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)  holder.messageLayout.getLayoutParams();
                       params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
           params.setMargins(0,0,R.dimen.chattab_llayout_chatlayout_marginright,0);
                        holder.messageLayout.setLayoutParams(params);
                        holder.messageLayout.setBackgroundResource(R.drawable.aqua);
                    }
                    else
                    {
                        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)  holder.messageLayout.getLayoutParams();
                        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                        params.setMargins(0, 0, R.dimen.chattab_llayout_chatlayout_marginright, 0);
                        holder.messageLayout.setLayoutParams(params);
                        holder.messageLayout.setBackgroundResource(R.drawable.grey);
                    }

if(broadcastResponse.getUid().equals(uId))
{
RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams)holder.messageLayout.getLayoutParams();
params.addRule(RelativeLayout.ALIGN\u PARENT\u RIGHT);
参数设置页边距(0,0,右图尺寸为chattab_llayout_chatlayout_marginright,0);
holder.messageLayout.setLayoutParams(参数);
holder.messageLayout.setBackgroundResource(R.drawable.aqua);
}
其他的
{
RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams)holder.messageLayout.getLayoutParams();
params.addRule(RelativeLayout.ALIGN\u PARENT\u LEFT);
参数设置边距(0,0,R.dimen.chattab_llayout_chatloayout_marginright,0);
holder.messageLayout.setLayoutParams(参数);
holder.messageLayout.setBackgroundResource(R.drawable.grey);
}
**

holder.messageLayout是id:llayout\u chatlayout在xml中的线性布局请参见图像它应显示与aqua相同的图像并向右对齐 而现在它的图像设置不正确。如何解决这个问题 ? 我在hdpi的所有文件夹中已经有了所有9个补丁图像, mdpi、xhdpi、xxhdpi

现在我在adapter类中以编程方式使用9补丁映像 但是,我使用java设置的图像在xml中无法正常工作 图像显示正确。如何将布局右对齐为灰色图像,而左对齐为aqua图像内部条件


您可以根据消息的发送方向设置您的密码。 我已经写了一些代码供你理解。 您可以将此代码放入适配器类中

 private void setAlignment(ViewHolder holder, boolean isOutgoing) {
        if (!isOutgoing) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.contentWithBG.getLayoutParams();
            layoutParams.gravity = Gravity.RIGHT;
            holder.contentWithBG.setLayoutParams(layoutParams);

            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.content.getLayoutParams();
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            holder.content.setLayoutParams(lp);

            layoutParams = (LinearLayout.LayoutParams) holder.txtInfo.getLayoutParams();
            layoutParams.gravity = Gravity.RIGHT;
            holder.txtInfo.setLayoutParams(layoutParams);
            if (holder.txtMessage != null) {
                holder.contentWithBG.setBackgroundResource(R.drawable.incoming_message_bg);
                layoutParams = (LinearLayout.LayoutParams) holder.txtMessage.getLayoutParams();
                layoutParams.gravity = Gravity.RIGHT;
                holder.txtMessage.setLayoutParams(layoutParams);
            } else {
                holder.contentWithBG.setBackgroundResource(android.R.color.transparent);
            }
        } else {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.contentWithBG.getLayoutParams();
            layoutParams.gravity = Gravity.LEFT;
            holder.contentWithBG.setLayoutParams(layoutParams);

            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.content.getLayoutParams();
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            holder.content.setLayoutParams(lp);

            layoutParams = (LinearLayout.LayoutParams) holder.txtInfo.getLayoutParams();
            layoutParams.gravity = Gravity.LEFT;
            holder.txtInfo.setLayoutParams(layoutParams);

            if (holder.txtMessage != null) {
                holder.contentWithBG.setBackgroundResource(R.drawable.outgoing_message_bg);
                layoutParams = (LinearLayout.LayoutParams) holder.txtMessage.getLayoutParams();
                layoutParams.gravity = Gravity.LEFT;
                holder.txtMessage.setLayoutParams(layoutParams);
            } else {
                holder.contentWithBG.setBackgroundResource(android.R.color.transparent);
            }
        }
    }
Xml:



什么是layoutParams?contentWithBG和txtMessage它们之间的区别是什么contentWithBG是一个线性布局,您必须将九个补丁图像作为背景,并将textview放在该线性布局中,但您将在该线性布局中放置文本消息。实际上,我的线性布局中有两个文本视图,并且应该是相同的线性布局包含背景图像和对齐属性请查看我的XML。您还可以设置两个文本视图或将第二个文本视图置于布局之外。试一试这个代码,它似乎是一个规模不好的9补丁的效果。确保9个补丁标记的宽度为1像素。透明背景上的纯黑色。此外,扩展名必须是.9.png,而不是.png。@FrankN.Stein yes.9.png已设置
<LinearLayout
            android:id="@+id/contentWithBackground"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@drawable/incoming_message_bg"
            android:paddingLeft="10dp"
            android:paddingBottom="10dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txtMessage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:maxWidth="250dp" />

        </LinearLayout>