2012-11-09 44 views
0

目前我有:變更指令programmaticaly

<TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="15:22:33 \nApr 13 1999" 
         android:textColor="#FFFFFFFF" 

     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/comment" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_margin="5dip" 
     android:background="@drawable/incoming_message_buble" 
     android:paddingLeft="10dip" 
     android:text="Hello bubbles!" 
     android:textColor="@android:color/primary_text_light" /> 

,它做工精細。

這裏是適配器代碼:

public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    if (row == null) { 
     LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = inflater.inflate(R.layout.activity_sms_row_layout, parent, false); 
    } 

    wrapper = (LinearLayout) row.findViewById(R.id.wrapper); 

    OneComment coment = getItem(position); 

    countryName = (TextView) row.findViewById(R.id.comment); 

    countryName.setText(coment.comment); 


    countryName.setBackgroundResource(coment.left ? R.drawable.incoming_message_buble : R.drawable.outgoing_message_buble); 
    wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT); 

    return row; 
} 

我必須改變順序,重力離開,我需要首先所示TextView的評論,以及重力的權利,保持XML設置textView1第二 。

請指教。

回答

2

我建議使用多個佈局,而不是不斷重新組織每個行的佈局。覆蓋這兩種方法:

@Override 
public int getItemViewType(int position) { 
    return getItem(position).left ? 0 : 1; 
} 

@Override 
public int getViewTypeCount() { 
    return 2; 
} 

和修改getView()

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if(getItemViewType == 0) { 
     // Use the left layout 
    } else { 
     // Use the right layout 
    } 
} 
+0

正確的解釋,THX! – user170317