我正在尝试以编程方式创建一个contentView,我无法理解为什么以下代码无法正常工作。这是代码:
public class UserProfile extends AppCompatActivity {
ScrollView scrollView;
LinearLayout contentnView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
ServerRequest serverRequest = new ServerRequest(this);
final String userid = intent.getStringExtra(PostPopUp.EXTRA_USER_PROFILE_ID);
serverRequest.getUserPostsInBackground(userid, new GetCallback() {
@Override
public void done(ArrayList<Document> docs) {
contentnView = new LinearLayout(UserProfile.this);
contentnView.setOrientation(LinearLayout.VERTICAL);
setContentView(contentnView);
RelativeLayout relativeLayoutWithView = new RelativeLayout(UserProfile.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
relativeLayoutWithView.setLayoutParams(params);
relativeLayoutWithView.setPadding(16, 16, 16, 16);
relativeLayoutWithView.setBackgroundColor(Color.WHITE);
FacebookSdk.sdkInitialize(getApplicationContext());
ProfilePictureView profilePictureView = new ProfilePictureView(UserProfile.this);
profilePictureView.setProfileId(userid);
profilePictureView.setId(View.generateViewId());
final HashMap<Integer, View> lines = new HashMap<>();
int i = 0;
for (Document post : docs) {
lines.put(i, new View(UserProfile.this));
i++;
}
RelativeLayout.LayoutParams paramsProfileImg = new RelativeLayout.LayoutParams(170, 170);
paramsProfileImg.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);
relativeLayoutWithView.addView(profilePictureView, paramsProfileImg);
for (int j = 0; j < textViews.size(); j++) {
RelativeLayout.LayoutParams paramsLine = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
if (j == 0) {
paramsLine.addRule(RelativeLayout.BELOW, profilePictureView.getId());
} else {
paramsLine.addRule(RelativeLayout.BELOW, lines.get(j - 1).getId());
}
paramsLine.setMargins(0, 16, 0, 0);
lines.get(j).setLayoutParams(paramsLine);
relativeLayoutWithView.addView(lines.get(j), paramsLine);
}
scrollView = new ScrollView(UserProfile.this);
scrollView.addView(relativeLayoutWithView);
contentnView.addView(scrollView);
}
});
}
}
我想要的是第一个视图(就像hr btw这样的一行)在齐下图像视图下面对齐(它确实如此)和其他视图使一个在另一个下方对齐。发生的情况是第一个视图在个人资料图片视图下方对齐,而所有其他视图都是堆叠到屏幕的开始。
有什么想法吗?
.. serverRequest.getUserPostsInBackground只是一个AsynkTask
答案 0 :(得分:1)
由于您正在使用relativelayout,因此您应该在预先添加的视图下对齐要添加的每个新视图。
我认为你这样做我不确定但是我不知道什么是行字段用于(一个视图列表?),如果它是 - 尝试并明确设置对于您创建的每个新视图的id,我怀疑它们都添加到顶部,因为id字段为0或类似的东西,因此您使用的布局参数无用(即它们都在视图下对齐,ID为-1或0,因为他们只有在你完成创建整个视图的操作后才能获得id)