簡體   English   中英

Android RelativeLayout沒有移動項目

[英]Android RelativeLayout not moving items

我正在嘗試創建一個采用'post'形式的viewGroup,我需要這個視圖在左上角有一個圖像,然后在該帖子的右邊有一個用戶名和內容,與其他人對齊然而我似乎無法看到讓布局移動。 他們只是保持重疊。

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);

}

我已按以下方式修改代碼並將其設置為空白活動的內容視圖:

    RelativeLayout rl = new RelativeLayout(context);
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Point p = new Point();
    wm.getDefaultDisplay().getSize(p);
    //
    // integer ID's for all the Views;
    int ivID = 0x0FD;
    int tvUserNameID = 0x0FE;
    int tvPostContentID = 0x0FF;
    //
    TextView tvUsername = new TextView(context);
    //
    tvUsername.setId(tvUserNameID); // Set the ID
    //
    tvUsername.setText(userName);
    TextView tvPostContent = new TextView(context);
    //
    tvPostContent.setId(tvPostContentID); // Set the ID
    //
    tvPostContent.setText(postContent);
    ImageView iv = new ImageView(context);
    //
    iv.setId(ivID); // Set the ID
    //
    RelativeLayout.LayoutParams lpTvUsername = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    //
    //lpTvUsername.addRule(RelativeLayout.RIGHT_OF,0);
    //lpTvUsername.addRule(RelativeLayout.ALIGN_TOP,0);
    //
    // Reference the ID not the Index;
    lpTvUsername.addRule(RelativeLayout.RIGHT_OF,ivID);
    lpTvUsername.addRule(RelativeLayout.ALIGN_TOP,ivID);
    //
    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);
    //
    // Reference the ID not the Index;
    lpTvPostContent.addRule(RelativeLayout.RIGHT_OF,ivID);
    lpTvPostContent.addRule(RelativeLayout.BELOW,tvUserNameID);
    //
    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);
    setContentView(rl);

主要是問題發生了,因為在設置LayoutParams時,您引用的是索引而不是Views ID

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM