0
我正在嘗試創建一個採用「發佈」形式的viewGroup,我需要此視圖在左上角有一個圖像,然後是該帖子右側的用戶名和內容,與彼此對齊,但我似乎無法讓佈局移動。他們只是保持重疊。Android RelativeLayout不移動項目
public ViewGroup generateViewGroup(Context context){
RelativeLayout rl = new RelativeLayout(context);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point p = new Point();
wm.getDefaultDisplay().getSize(p);
TextView tvUsername = new TextView(context);
tvUsername.setText(userName);
TextView tvPostContent = new TextView(context);
tvPostContent.setText(postContent);
ImageView iv = new ImageView(context);
RelativeLayout.LayoutParams lpTvUsername = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lpTvUsername.addRule(RelativeLayout.RIGHT_OF,0);
lpTvUsername.addRule(RelativeLayout.ALIGN_TOP,0);
tvUsername.setLayoutParams(lpTvUsername);
RelativeLayout.LayoutParams lpTvPostContent = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lpTvPostContent.addRule(RelativeLayout.RIGHT_OF,0);
lpTvPostContent.addRule(RelativeLayout.BELOW,1);
tvPostContent.setLayoutParams(lpTvPostContent);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
if (userPic==null) Log.e("generateViewGroup","userPic is null");
iv.setImageDrawable(userPic);
rl.addView(iv, 0);
rl.addView(tvUsername,1);
rl.addView(tvPostContent);
rl.setLayoutParams(lp);
}
您不想誇大XML的具體原因是什麼? – Droidekas 2015-03-03 05:46:49
我想堅持java,因爲我們有許多其他類與這一個(http請求等)一起工作,所以它不會很容易在XML中做 – 2015-03-03 06:40:30