【问题标题】:align View programmatically doesn't work以编程方式对齐视图不起作用
【发布时间】:2015-06-10 06:55:05
【问题描述】:

这是我的布局:

  • 滚动视图
    • 线性布局
      • 相对布局
        • 图像视图
        • 文本视图
      • 相对布局
        • 图像视图
        • 文本视图


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

    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 idimageView.setId(R.id.txtImage); 并将规则设置为 txtNamenamelp.addRule(RelativeLayout.RIGHT_OF, R.id.txtImage);,但它不起作用。
这是我的输出:

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>

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      我会这样做:

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

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

      【讨论】:

        【解决方案3】:

        这条线对我有用,使用

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

        而不是

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

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

        imagelp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-04-01
          • 2016-07-17
          • 1970-01-01
          • 1970-01-01
          • 2014-01-01
          • 1970-01-01
          • 2018-04-02
          • 1970-01-01
          相关资源
          最近更新 更多