2012-10-31 3 views
2

메시지가 왼쪽과 오른쪽으로 표시되는 목록보기를 사용하여 채팅과 같은보기를 만들려고합니다. 왼쪽 메시지의 마크 업리스트 레이아웃과 같은 Android 레이아웃 채팅

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="5dp" 
    android:gravity="left"> 

    <TextView 
     android:id="@+id/MessageListItemText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#000000" 
     android:background="@drawable/RoundedCornersBlue" 
     android:padding="8dp" 
     android:text="realy long realy long realy long realy long realy long realy long realy long realy long " 
     android:layout_marginRight="5dp" /> 
    <TextView 
     android:id="@+id/MessageListItemDate" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:textColor="#cbc4b1" 
     android:text="23:11" 
     android:gravity="center_horizontal|center_vertical" /> 
</LinearLayout> 

이지만 제 텍스트 뷰 (타임 스탬프)를 누락한다.

http://i.stack.imgur.com/JxDXQ.jpg

오른쪽 메시지가 나도 그것을 원하는이 하나가 보인다

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="5dp" 
    android:gravity="right"> 
    <TextView 
     android:id="@+id/MessageListItemDate" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:textColor="#cbc4b1" 
     android:text="12:34" 
     android:gravity="center_horizontal|center_vertical" /> 
    <TextView 
     android:id="@+id/MessageListItemText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#000000" 
     android:text="realy long realy long realy long realy long realy long realy long realy long realy long " 
     android:background="@drawable/RoundedCornersBlue" 
     android:padding="8dp" 
     android:layout_marginLeft="5dp" /> 
</LinearLayout> 

http://i.stack.imgur.com/1MnyW.jpg

다음과 같은 마크 업을 가지고있다. 왼쪽과 오른쪽의 유일한 차이점은 LinearLayout의 android : gravity와 LinearLayout의 textviews 순서입니다.

내가 뭘 잘못하고 있는지 아는 사람이 있습니까? 아니면 내가 디자인

의이 유형을 달성 할 수있는 방법 당신은 채팅의 두 개의 서로 다른 XML을 만들 필요가 없습니다, 미하이

+2

(질문과 관련이 없지만 여전히) 레이아웃이 수평이므로 선형 레이아웃의 중력은 무시됩니다. 수직 관련 중력 만이 사용될 수있다. (그리고 그 반대, 수직 방향에 대한 수평 중력) – njzk2

답변

1

을 주셔서 감사합니다. 당신은 이것을 하나의 목표로 달성 할 수 있습니다.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/txtsenderdt" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="left|center" 
      android:text="07:03" 
      android:textColor="@android:color/darker_gray" 
      android:textSize="18dp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/txtsender" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="left" 
      android:background="@drawable/chat1" 
      android:text="realy long realy long realy long realy long realy long realy long realy long realy long realy long realy long realy long realy long realy long realy long realy long realy long" 
      android:textSize="18dp" /> 
    </LinearLayout> 

</LinearLayout> 
+1

고마워,하지만 이건 내 질문에 대답하지 않습니다. 메시지 옆에 타임 스탬프를 추가하고 싶습니다. 왼쪽 메시지의 경우 타임 스탬프가 메시지 오른쪽에 있어야합니다. 올바른 메시지의 경우 다른 방향입니다. – Mihai

+0

내 대답을 업데이트했습니다. 확인하고 알려주세요. 문제가있는 경우 – juned

+0

도움이되지 않습니다. 정말로 긴 텍스트를 추가하면 어떻게됩니까? 게다가, 내 디자인은 메시지에 대해 다른 레이아웃을 사용하여 완료했습니다. – Mihai

0

당신이 메시지 왼쪽 orientation="horizontal" 처음 TextViewLinearLayout을 사용하고 있기 때문에 가능한 모든 공간을 채 웁니다. 그래서 당신은 내가 최근에 동일한 문제가 발생이

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="5dp" 
    android:gravity="left"> 

    <TextView 
     android:id="@+id/MessageListItemDate" 
     android:layout_width="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_height="fill_parent" 
     android:textColor="#cbc4b1" 
     android:text="23:11" 
     android:gravity="center_horizontal|center_vertical" /> 

    <TextView 
     android:id="@+id/MessageListItemText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#000000" 
     android:layout_toLeftOf="@id/MessageListItemDate" 
     android:background="@drawable/RoundedCornersBlue" 
     android:padding="8dp" 
     android:text="realy long realy long realy long realy long realy long realy long realy long realy long " 
     android:layout_marginRight="5dp" /> 
</RelativeLayout> 
+0

고마워요,하지만 긴 텍스트에서만 작동합니다. 짧은 텍스트를 추가하십시오. – Mihai

+0

짧은 또는 긴 메시지인지 여부에 관계없이 항상 메시지 옆에 타임 스탬프를 표시하고 싶습니다. – Mihai

+0

좋아,'android : layout_width = "wrap_content"'를 android : layout_width로 바꾸십시오. = "MessageListItemText'의"fill_parent "' – vasart

2

처럼 RelativeLayoutLinearLayout을 변경해야합니다. 얼마 전에 답을 찾았지만 다른 사람들에게 도움이 될 수 있습니다. 여기에 내가 그 처리 방법은 다음과 같습니다이 모든 것을 왼쪽에 서 기본적으로 내 conversation_item에 대한 XML 코드

: 복제 XMLS 대신 다음

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/conversationItemRelativeLayout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:paddingTop="5dp" 
       android:paddingBottom="5dp" 
       android:paddingLeft="11dp" 
       android:paddingRight="11dp" 
       > 

    <ImageView 
      android:id="@+id/conversationUserImage" 
      android:layout_width="60dp" 
      android:layout_height="60dp" 
      android:background="@drawable/my_ears_placeholder_grey_background" 
      android:layout_alignBottom="@+id/conversationBubble" 
      /> 

    <TextView 
      android:id="@+id/conversationDate" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Time Ago" 
      android:layout_below="@id/conversationUserImage" 
      android:layout_marginTop="5dp" 

      /> 

    <TextView 
      android:id="@+id/conversationBubble" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/conversationUserImage" 
      android:text="Exemple Text" 
      android:background="@drawable/bubbleleft" 
      android:gravity="center" 
      /> 

    <ImageView 
      android:id="@+id/messagePicture" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/conversationUserImage" 
      android:gravity="center" 
      /> 

</RelativeLayout> 

, 내가 함께, 코드의 레이아웃 변경을 설정 내 사용자 지정 어댑터에서 호출하는 메서드 :

private View getConversationItem(Message message) { 

     View view = LayoutInflater.from(ConversationActivity.this).inflate(R.layout.conversation_item, null); 
     ... 

     if (message.iAmOwner()) {   
       conversationBubble.setBackground(getResources().getDrawable(R.drawable.bubbleleft)); 
       messagePicture.setBackground(getResources().getDrawable(R.drawable.bubbleleft));        
       ImageLoader.getInstance().displayImage(myPictureURL, conversationUserImage, options); 
       ... 
     } 
     else {  
       ImageLoader.getInstance().displayImage(senderPictureURL, conversationUserImage, options); 
       conversationBubble.setBackground(getResources().getDrawable(R.drawable.bubbleright)); 
       messagePicture.setBackground(getResources().getDrawable(R.drawable.bubbleright)); 


      RelativeLayout.LayoutParams conversationUserImageParams = (RelativeLayout.LayoutParams)  conversationUserImage.getLayoutParams(); 
      conversationUserImageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
      conversationUserImage.setLayoutParams(conversationUserImageParams); 

      RelativeLayout.LayoutParams conversationDateParams = (RelativeLayout.LayoutParams)conversationDate.getLayoutParams(); 
      conversationDateParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
      conversationDate.setLayoutParams(conversationDateParams); 

      RelativeLayout.LayoutParams conversationBubbleParams = (RelativeLayout.LayoutParams) conversationBubble.getLayoutParams(); 
      conversationBubbleParams.addRule(RelativeLayout.LEFT_OF,R.id.conversationUserImage); 


      RelativeLayout.LayoutParams messagePictureParams = (RelativeLayout.LayoutParams) messagePicture.getLayoutParams(); 
      messagePictureParams.addRule(RelativeLayout.LEFT_OF,R.id.conversationUserImage); 

      messagePictureParams.removeRule(RelativeLayout.RIGHT_OF); 
      conversationBubbleParams.removeRule(RelativeLayout.RIGHT_OF); 

      conversationBubble.setLayoutParams(conversationBubbleParams); 
      messagePicture.setLayoutParams(messagePictureParams); 

     } 

     return view; 
    } 

희망이 도움이됩니다. 건배

+0

xml의 모든 수정 내용이 코드를 수정해야하므로 두 xml 접근 방식으로 전환 중입니다. 내가 XML에 더 많은 세부 사항을 추가함에 따라 문제가 발생하기 시작했다. 앞으로 발생할 수있는 성능 문제에 대해서는 확신 할 수 없지만 앞으로는 관리가 훨씬 쉬워 질 것입니다. – Stanislasdrg

관련 문제