我的XML布局的TextView的位置默认为在View的左侧。

  <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();
                }
            });
        }
    }


}

最佳答案

如果视图的宽度为"wrap_content",则无法在左侧或右侧移动文本。您应该使它们的宽度超出您的文本,以setGravity()可以更改任何内容。

另外,您必须知道gravitylayout_gravity之间的区别。第一个影响Textview内部的文本,第二个相对影响View的布局。

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

10-07 19:19
查看更多