以编程方式在后台设置布局9补丁图像不能正常工作

时间:2015-09-18 06:07:09

标签: 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_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);
                    }

**

holder.messageLayout是在xml中具有id:llayout_chatlayout的LinearLayout看到图像它应该显示与aqua相同的图像并且右对齐  而现在它没有正确设置图像。如何解决这个问题  ?我已经在hdpi的所有文件夹中拥有所有9个补丁图像,  mdpi,xhdpi,xxhdpi

现在我在我的适配器类中使用9个补丁图像  但是使用java设置的图像在xml时工作不正常  图像显示正确。我可以将布局与灰色图像对齐,同时在条件内与水色图像对齐。

image issue

1 个答案:

答案 0 :(得分:0)

您可以按照消息的方向设置。 我已经提供了一些代码供您理解。 您可以将此代码放在适配器类中。

 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:

<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>