【问题标题】:Android RelativeLayout not moving itemsAndroid RelativeLayout 不移动项目
【发布时间】:2015-03-03 14:52:20
【问题描述】:

我正在尝试创建一个采用“帖子”形式的视图组,我需要此视图在左上角有一个图像,然后在该帖子的右侧有一个用户名和内容,但我彼此对齐似乎无法让布局移动。它们只是保持重叠。

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 的任何具体原因?
  • 我试图坚持使用 java,因为我们有许多其他类与这个类(http 请求等)一起工作,所以在 xml 中做到这一点并不容易

标签: java android android-relativelayout


【解决方案1】:

我已经通过如下方式修改了代码,并将其设置为空白Activity的内容视图:

    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 时您引用的是 index 而不是 Views ID

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    相关资源
    最近更新 更多