我的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位置更改为屏幕右侧?我尝试使用&#39; Gravity&#39;但它并没有改变立场。
这是通过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 :(得分:1)
如果您的观看次数有"wrap_content"
宽度,则无法向左或向右移动文字。你应该让它们比文本宽setGravity()
可以改变任何东西。
此外,您必须了解gravity
和layout_gravity
之间的区别。第一个是影响Textview中的文本,第二个是相对于Textview相对于视图的布局。
setGravity()
方法影响gravity
参数,而不是layout_gravity
。
答案 1 :(得分:1)