以编程方式对齐视图不起作用

时间:2015-06-10 06:55:05

标签: android

这是我的布局:

  • 滚动型
    • 的LinearLayout
      • RelativeLayout的
        • ImageView的
        • 的TextView
      • RelativeLayout的
        • ImageView的
        • 的TextView


这是我创建布局和视图的函数

    public void createUI(int count)
    {
        ScrollView scrollView = new ScrollView(this);
        ScrollView.LayoutParams svp = new ScrollView.LayoutParams(
                ScrollView.LayoutParams.WRAP_CONTENT, ScrollView.LayoutParams.WRAP_CONTENT);
        scrollView.setLayoutParams(svp);

        LinearLayout linearLayout = new LinearLayout(this);
        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setLayoutParams(llp);

        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 110, getResources().getDisplayMetrics());
        int ivSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getResources().getDisplayMetrics());

        for (int i = 0; i < 2; ++i)
        {
            RelativeLayout relativeLayout = new RelativeLayout(this);
            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT, height);
            relativeLayout.setLayoutParams(rlp);

            ImageView imageView = new ImageView(this);
            imageView.setId(R.id.txtImage);
            imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.myborder));
            RelativeLayout.LayoutParams imagelp = new RelativeLayout.LayoutParams(ivSize, ivSize);
            imagelp.addRule(RelativeLayout.ALIGN_PARENT_START);
            imageView.setLayoutParams(imagelp);


            TextView txtName = new TextView(this);
            imageView.setId(R.id.txtName);
            txtName.setText("Abu Baka");
            RelativeLayout.LayoutParams namelp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            namelp.addRule(RelativeLayout.RIGHT_OF, R.id.txtImage);
            txtName.setLayoutParams(namelp);

            relativeLayout.addView(imageView);
            relativeLayout.addView(txtName);
            linearLayout.addView(relativeLayout);
        }
        scrollView.addView(linearLayout);
        setContentView(scrollView);
    }

我以编程方式设置了imageView id imageView.setId(R.id.txtImage);并将规则设置为txtName namelp.addRule(RelativeLayout.RIGHT_OF, R.id.txtImage);,但它不起作用。
这是我的输出:
enter image description here

单词Abu Baka假设是imageView的权利,但它不是......如何解决这个问题?或者我的代码有什么问题吗?
这是我的ids.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="txtImage" type="id"/>
    <item name="txtName" type="id"/>
    <item name="txtSince" type="id"/>
    <item name="txtComment" type="id"/>
</resources>

3 个答案:

答案 0 :(得分:0)

将子视图添加到其父视图时,使用布局参数调用该方法:

relativeLayout.addView(imageView, imageView.getLayoutParams());
relativeLayout.addView(txtName, , txtName.getLayoutParams());
linearLayout.addView(relativeLayout, relativeLayout.getLayoutParams());

请确保您已在视图上设置它们或在addView()方法中添加新的布局参数。

答案 1 :(得分:0)

我会这样做如下:

RelativeLayout.LayoutParams namelp = (RelativeLayout.LayoutParams) txtName.getLayoutParams();
namelp.addRule(RelativeLayout.RIGHT_OF, R.id.txtImage);
txtName.setLayoutParams(namelp);

另外,您错过了设置txtName的ID。

答案 2 :(得分:0)

这条线对我有用,使用

namelp.addRule(RelativeLayout.RIGHT_OF, imageView.getId());

而不是

namelp.addRule(RelativeLayout.RIGHT_OF, R.id.txtImage);

仅供参考:当我使用2.3版本进行测试时,进行了另一项更改(因为我不需要我猜)

imagelp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);