android以编程方式更改对齐

时间:2016-07-20 08:16:26

标签: android android-layout

这是我的xml

public void getComments(final String postId, final Activity activity, final View fragmentView, final View progressOverlay) {
    final Firebase commentsRef = firebaseRef.child("/comments");
    Firebase linkRef = firebaseRef.child("/posts/" + postId);
    linkRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            System.out.println("Test 1");
            if (progressOverlay.getVisibility() == View.VISIBLE) {
                progressOverlay.setVisibility(View.GONE);
                AndroidUtils.animateView(progressOverlay, View.GONE, 0, 200);
                fragmentView.findViewById(R.id.rv_view_comments).setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    linkRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            commentsRef.child(dataSnapshot.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Comment comment = dataSnapshot.getValue(Comment.class);
                    System.out.println("Test 2");
                    application.getCommentsRecyclerViewAdapter().getCommentsList().add(comment);
                    application.getCommentsRecyclerViewAdapter().notifyDataSetChanged();
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
} 

和结果

enter image description here

因为我们看到textView对齐到左边 现在,当我以编程方式将其更改为正确时,它将像这样

enter image description here

左侧对齐,右侧对齐xml,右侧对齐,以编程方式为什么imageView和textView之间有空格

我在xml中用于对齐的内容

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:orientation="horizontal">


<customfonts.RoundedImageView
android:id="@+id/profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ssz"/>

<customfonts.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/circulo"
android:layout_alignLeft="@id/profile"
android:gravity="center_vertical|center_horizontal"
android:text="2"
android:textColor="#fff"
android:textSize="13sp"
android:id="@+id/num" />
</RelativeLayout>

以及我用于以编程方式对齐的内容

android:layout_alignRight="@id/profile"

其中RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)Num.getLayoutParams(); params.addRule(RelativeLayout.LEFT_OF, 0); params.addRule(RelativeLayout.RIGHT_OF, R.id.profile); Num.setLayoutParams(params); 是TextView

1 个答案:

答案 0 :(得分:1)

请给出-20dp

的负余量