2015-01-04 69 views
0

我的XML佈局的TextView默認位於視圖的左側。Android:以編程方式更改TextView位置(左或右)?

<TextView 
    android:id="@+id/user_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/thumbnail" 
    android:layout_toRightOf="@+id/thumbnail" 
    android:text="John Doe" 
    android:textColor="#040404" 
    android:typeface="sans" 
    android:textSize="15dip" 
    android:textStyle="bold"/> 

如何將TextView位置更改爲屏幕的右側?我試圖用'重力'來做到這一點,但它並沒有改變位置。

這是從一個GridView適配器完成的,但我相信這不會影響任何東西。 我的代碼:

public class CommentsAdapter extends ArrayAdapter<Comment_FB>{ 
    ArrayList<Comment_FB> comments; 
    Context ctx; 
    int resource; 
    String Fname; 
    String Lname; 


    public CommentsAdapter(Context context, int resource,ArrayList<Comment_FB> commentsList, String Fname, String Lname) { 
     super(context, resource, commentsList); 
     // TODO Auto-generated constructor stub 
     this.comments = commentsList; 
     this.ctx = context; 
     this.resource = resource; 
     this.Fname = Fname; 
     this.Lname = Lname; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     if(comments.size() == 0){ 
      return 0; 
     }else{ 
      return comments.size(); 
     } 
    } 


    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     View child = convertView; 
     RecordHolder holder; 
     LayoutInflater inflater = ((Activity) ctx).getLayoutInflater(); // inflating your xml layout 

     if (child == null) {    
      child = inflater.inflate(R.layout.comment_single, parent, false); 
      holder = new RecordHolder(); 
      holder.user = (TextView) child.findViewById(R.id.user_name); 
      holder.comment = (TextView) child.findViewById(R.id.user_comment); 
      holder.date = (TextView) child.findViewById(R.id.date); 
      holder.image = (ImageView) child.findViewById(R.id.list_image); 
      child.setTag(holder); 
     }else{ 
      holder = (RecordHolder) child.getTag(); 
     } 

     final Comment_FB feedObj = comments.get(position); // you can remove the final modifieer. 


     holder.comment.setText(feedObj.getComment()); 
     holder.user.setText(feedObj.getCommenter()); 
     holder.date.setText(feedObj.getDate()); 

     SharedPreferences pref = ctx.getSharedPreferences("JezzaPref", 0); 
     String curUser = pref.getString("current_user", null); 
     if(feedObj.getCommenter().equals(curUser)) 
     { 
      holder.comment.setBackgroundResource(R.drawable.bubble_green); 
      holder.comment.setGravity(Gravity.RIGHT); 
      holder.user.setGravity(Gravity.RIGHT); 
      holder.date.setGravity(Gravity.LEFT); 
     } 

     return child; 
    } 

    static class RecordHolder { 
     public TextView date; 
     public TextView user; 
     public TextView comment; 
     public ImageView image; 
    } 



    @Override 
    public void notifyDataSetChanged() { // you can remove this.. 
     // TODO Auto-generated method stub  
     if(getCount() == 0){ 
      //show layout or something that notifies that no list is in.. 
     }else{ 
      // this is to make sure that you can call notifyDataSetChanged in any place and any thread 
      new Handler(getContext().getMainLooper()).post(new Runnable() { 

       @Override 
       public void run() { 
        // TODO Auto-generated method stub 
        CommentsAdapter.super.notifyDataSetChanged(); 
       } 
      }); 
     } 
    } 

}

+0

請告訴我們更多的代碼從你的活動 – 2015-01-04 11:55:28

+0

@MounirElfassi編輯 – Pjayness 2015-01-04 12:08:31

回答

1

如果你的觀點有"wrap_content"寬度內左或右,你不能移動文本。你應該讓它們比文字更寬,以便setGravity()可以改變任何內容。

此外,您必須知道gravitylayout_gravity之間的差異。第一個影響到Textview中的文本,第二個影響Textview相對視圖的佈局。

setGravity()方法影響到gravity參數,而不是layout_gravity

For example:

相關問題