简体   繁体   中英

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

My XML Layout's TextView's are positioned to be by default at the left hand side of the 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"/>

How can I change the TextView position to be at the right hand side of the screen? I tried to do this using 'Gravity' but it doesn't change the position.

This is done from a GridView Adapter but I'm sure that doesn't affect anything. My code:

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

}

If your views have "wrap_content" width you cannot move the text inside left or right. You should make them wider than your text to setGravity() could change anything.

Also, you have to know difference between gravity and layout_gravity . The first one is affecting to the text inside your Textview and the second one is affecting to the Textview relatively the views's layout.

The setGravity() method is affecting to the gravity parameter, not layout_gravity .

例如:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM