Android:以编程方式更改 TextView 位置(左或右)?

Android: Changing TextView Position (Left or Right) Programmatically?

我的 XML 布局的 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 的位置更改为屏幕的右侧?我尝试使用 'Gravity' 来做到这一点,但它并没有改变位置。

这是通过 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();
                }
            });
        }
    }

}

如果您的视图有 "wrap_content" 宽度,则您不能向左或向右移动文本。你应该让它们比你的文字更宽 setGravity() 可以改变任何东西。

此外,你必须知道gravitylayout_gravity之间的区别。第一个影响 Textview 中的文本,第二个影响 Textview 相对于视图的布局。

setGravity() 方法影响 gravity 参数,而不是 layout_gravity

指定当文本小于视图时如何按视图的 x- and/or y 轴对齐文本。

*检查父尺寸 *