【问题标题】:Android: Changing TextView Position (Left or Right) Programmatically?Android:以编程方式更改 TextView 位置(左或右)?
【发布时间】:2015-01-04 11:22:41
【问题描述】:

默认情况下,我的 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 的位置更改为屏幕右侧?我尝试使用“重力”来做到这一点,但它不会改变位置。

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

}

【问题讨论】:

  • 请向我们展示更多来自您活动的代码
  • @MounirElfassi 已编辑

标签: java android xml position gravity


【解决方案1】:

如果您的视图具有"wrap_content" 宽度,则您不能将文本向左或向右移动。您应该使它们比setGravity() 的文本更宽,这可能会改变任何事情。

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

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

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 2016-07-17
    • 2018-11-05
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 2015-04-09
    • 2016-04-24
    相关资源
    最近更新 更多