2016-09-17 3 views
3

채팅 앱에서 작업 중이며 채팅보기에서 메시지를 왼쪽과 오른쪽으로 표시해야합니다. 하지만 안드로이드 프로그래밍에서 꽤 새로 왔기 때문에 나는 이것을 달성 할 수 없다. 여기에 내가 무엇입니까 무엇 :안드로이드에서 목록보기 항목을 왼쪽에서 오른쪽으로 프로그래밍 방식으로 정렬하는 방법

enter image description here

여기 라인 항목을 표시하는 데 사용되는 내 chatbubble.xml입니다. 그 어댑터와 함께 사용되는 메인리스트 뷰 파일 activity_chat_list.xml

<?xml version="1.0" encoding="utf-8"?> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/bubble_layout_parent" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <LinearLayout 
       android:id="@+id/bubble_layout" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/bg_msg1"> 

       <TextView 
        android:id="@+id/message_text" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:maxEms="12" 
        android:layout_gravity="center" 
        android:text="Hi! new message" 
        android:textColor="@android:color/primary_text_light" /> 
      </LinearLayout> 

     </LinearLayout> 

그리고 ChatApapter.java

여기
@Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = layoutInflater.inflate(resource, parent, false); 

      Holder holder = new Holder(); 
      holder.txtMsg = (TextView) view.findViewById(R.id.message_text); 

      HashMap<String,String> hashMap = new HashMap<String,String>(); 
      hashMap = chats.get(position); 

      LinearLayout layout = (LinearLayout) view.findViewById(R.id.bubble_layout); 
      LinearLayout parent_layout = (LinearLayout) view.findViewById(R.id.bubble_layout_parent); 

      layout.setPadding(20,20,20,20); 

      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); 
      params.setMargins(0,0,0,20); 
      layout.setLayoutParams(params); 

      if(hashMap.get("is_mine").equals("yes")) { 
       layout.setBackgroundResource(R.drawable.bg_msg1); 
       parent_layout.setGravity(Gravity.RIGHT); 
      } else { 
       layout.setBackgroundResource(R.drawable.bg_msg2); 
       parent_layout.setGravity(Gravity.LEFT); 
      } 

      holder.txtMsg.setText(hashMap.get("message")); 
      holder.txtMsg.setTextColor(Color.BLACK); 

      return view; 
     } 

의 getView의 방법이다.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.mychatapp.UserChat"> 

    <ListView 
     android:id="@+id/msgListView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/form" 
     android:divider="@null" 
     android:dividerHeight="0dp" 
     android:paddingBottom="10dp" 
     android:text="Hello World" /> 

    <LinearLayout 
     android:id="@+id/form" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:background="#91f1f1f1" 
     android:paddingBottom="2dp"> 

     <EditText 
      android:id="@+id/messageEditText" 
      android:layout_width="252dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:layout_toLeftOf="@+id/sendMessageButton" 
      android:layout_weight="0.72" 
      android:ems="10" 
      android:maxHeight="80dp" /> 

     <ImageButton 
      android:id="@+id/sendMessageButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/send_button" 
      android:text="d" /> 

    </LinearLayout> 


</RelativeLayout> 

누군가가 메시지를 왼쪽에서 오른쪽으로 만들 수 있도록 도와 줄 수 있습니다. 미리 감사드립니다.

Lineqarlayout ll =new Linear Layout(); 
    LayoutParams lparams = new LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     TextView tvIncoming=new TextView(this); 
    tvIncoming.setLayoutParams(lparams); 
    tvIncoming.setGravity(GRAVITY.RIGHT); 
    tvIncoming.setText("test"); 
    this.ll.addView(tv); 
    //similarly tvOutgoing with gravity left 

    if(hashMap.get("is_mine").equals("yes")) { 
     tvOutgoing.setBackgroundResource(R.drawable.bg_msg1); 
     holder.txtMsg.setText(hashMap.get("message")); 
     } else { 
     tvincogoing.setBackgroundResource(R.drawable.bg_msg1); 
     holder.txtMsg.setText(hashMap.get("message")); 
     } 

이 도움이된다면 알려줘 같은 새로운 선형 레이아웃을 만들 : 대신 동적 텍스트 뷰를 사용해보십시오 목록보기를 사용

+0

설정 중력 = "시작"& "종료"?? –

+0

@PrateekRathore 시작과 끝의 예를 보여줄 수 있습니까? 그래서 코드로 구현할 수 있습니다. – Ankit

+0

xml 파일에서 동적으로 작업 해보십시오. –

답변

0

: 편집 : 활동이 코드 (또는 의사)를 사용하지 in adapter

+0

아니요, 작동하지 않습니다. 텍스트가 항상 왼쪽에 표시됩니다. 왜 그런지 몰라? – Ankit

+0

@Ankit 같은 listview를 사용하거나 동적 레이아웃을 사용하고 있습니까? – DevKRos

+0

동일한 목록보기를 사용하고 있습니다. – Ankit

0

is_mine이 yes이면 두 개의 다른 textview를 사용하고 다른 하나는 textview에 텍스트를 설정하고 그 반대의 경우도 마찬가지입니다.

1

gravity 대신 layout_gravity을 왼쪽이나 오른쪽으로 설정해야합니다.

내가 How to set layout_gravity programmatically?

예에서 개념을 복사하고 있습니다 (경고, 코드가 테스트되지 않음) : 또는

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); 


    if(hashMap.get("is_mine").equals("yes")) { 
     layout.setBackgroundResource(R.drawable.bg_msg1); 
     params.gravity = Gravity.RIGHT; 
     parent_layout.setParams(params); 
    } else { 
     layout.setBackgroundResource(R.drawable.bg_msg2); 
     params.gravity = Gravity.LEFT; 
     parent_layout.setParams(params); 
     parent_layout.setGravity(Gravity.LEFT); 
    } 

, 당신은 2 개의 다른 XMLS을하고 XML 자체 내부 layout_gravity을 할당하고, 팽창 각 행에 적합한 레이아웃.

+0

나를 위해 작동하지 않습니다. Thanks – Ankit

+0

@Ankit'bubble_layout_parent'의 layout_width를'match_parent'로 변경하고'bubble_layout_parent' 대신'bubble_layout'의'layout_gravity'를 설정해보십시오. –

+0

나는 이것을 시도했지만 여전히 작동하지 않습니다. 좀 더 이해하기 위해 main listview.xml을 게시해야합니까? – Ankit

관련 문제