遇到问题:在使用相对布局创建动态视图时。

huangapple go评论75阅读模式
英文:

Facing issue While making dynamic views using Relative layout

问题

我正在使用相对布局来动态构建视图/网格...

例如:如果有1个用户,

[具有1个用户的网格]1

如果有2个用户,

[具有2个用户的网格]2

如果有3个用户,

[具有3个用户的网格]3

如果有4个用户,
2个在上面,2个在下面

如果有5个用户,
[具有5个用户的网格]4

如果有6个用户。
[具有6个用户的网格]5

我的代码如下:

private RelativeLayout.LayoutParams[] getgrid(int size) {

  int width = Math.max(getMeasuredWidth(),getMeasuredHeight());
    int height = Math.min(getMeasuredWidth(),getMeasuredHeight());

    RelativeLayout.LayoutParams[] array =
            new RelativeLayout.LayoutParams[size];

    for (int i = 0; i < size; i++) {
        if (i == 0) {
            array[0] = new RelativeLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
            array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
        } else if (i == 1) {
            array[1] = new RelativeLayout.LayoutParams(width / 2, height);
            array[0].width = array[1].width;
            array[1].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(0)).getId());
            array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
          //  array[1].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
        } else if (i == 2) {
            array[i] = new RelativeLayout.LayoutParams(width / 3, height);
            array[1].width = array[i].width;
            array[0].width = array[i].width;
            //array[i].addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            array[i].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(1)).getId());
        } else if (i == 3) {
            array[i] = new RelativeLayout.LayoutParams(width / 2, height / 2);
            array[0].width = array[i].width;
            array[1].width = array[i].width;
            array[i - 1].width = array[i].width;
            array[0].height = array[i].height;
            array[1].height = array[i].height;
            array[i - 1].height = array[i].height;
            array[i - 1].addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            array[i].addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
            array[i - 1].addRule(RelativeLayout.BELOW, mUserViewList.get(mUidList.get(0)).getId());
            array[i].addRule(RelativeLayout.BELOW, mUserViewList.get(mUidList.get(1)).getId());
            array[i].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(i - 1)).getId());
        } else if (i == 4) {
            array[i] = new RelativeLayout.LayoutParams(width / 3, height / 2);
            array[0].width = array[i].width;
            array[1].width = array[i].width;
            array[2].width = array[i].width;
            array[3].width = array[i].width;
            array[0].setMargins(width / 6, 0, 0, 0);
            array[i].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(i - 1)).getId());
            array[i].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        } else if (i == 5) {
            array[i] = new RelativeLayout.LayoutParams(width / 3, height / 2);
            array[0].setMargins(0, 0, 0, 0);
            array[i].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(1)).getId());
            array[i].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);

        }
    }

     return array;
}

问题在于相对布局旁边还有另一个视图,因此它不能均匀分布并且可能出现变形(变形意味着某些网格没有覆盖到整个高度)。

请参考以下链接:
[问题6]6

[问题3]7
以此类推

请提供帮助,提前感谢。

英文:

I'm using relative layout for building the views/grid dynamically...

ex: if there is 1 user then,

[grid with users 1]1

if there is 2 user then ,

[grid with users 2]2

if there is 3 user then,

[grid with user 3]3

if there are 4 user then ,
2 above and 2 below

if there are 5 then,
[grid with user 5]4

if there are 6 then.
[grid with user 6]5

my code is ,

 private RelativeLayout.LayoutParams[] getgrid(int size) {
int width = Math.max(getMeasuredWidth(),getMeasuredHeight());
int height = Math.min(getMeasuredWidth(),getMeasuredHeight());
RelativeLayout.LayoutParams[] array =
new RelativeLayout.LayoutParams[size];
for (int i = 0; i < size; i++) {
if (i == 0) {
array[0] = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
} else if (i == 1) {
array[1] = new RelativeLayout.LayoutParams(width / 2, height);
array[0].width = array[1].width;
array[1].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(0)).getId());
array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
//  array[1].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
} else if (i == 2) {
array[i] = new RelativeLayout.LayoutParams(width / 3, height);
array[1].width = array[i].width;
array[0].width = array[i].width;
//array[i].addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
array[i].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(1)).getId());
} else if (i == 3) {
array[i] = new RelativeLayout.LayoutParams(width / 2, height / 2);
array[0].width = array[i].width;
array[1].width = array[i].width;
array[i - 1].width = array[i].width;
array[0].height = array[i].height;
array[1].height = array[i].height;
array[i - 1].height = array[i].height;
array[i - 1].addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
array[i].addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
array[i - 1].addRule(RelativeLayout.BELOW, mUserViewList.get(mUidList.get(0)).getId());
array[i].addRule(RelativeLayout.BELOW, mUserViewList.get(mUidList.get(1)).getId());
array[i].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(i - 1)).getId());
} else if (i == 4) {
array[i] = new RelativeLayout.LayoutParams(width / 3, height / 2);
array[0].width = array[i].width;
array[1].width = array[i].width;
array[2].width = array[i].width;
array[3].width = array[i].width;
array[0].setMargins(width / 6, 0, 0, 0);
array[i].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(i - 1)).getId());
array[i].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
} else if (i == 5) {
array[i] = new RelativeLayout.LayoutParams(width / 3, height / 2);
array[0].setMargins(0, 0, 0, 0);
array[i].addRule(RelativeLayout.RIGHT_OF, mUserViewList.get(mUidList.get(1)).getId());
array[i].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
}
}
return array;
}

the issue is there is another view right besides the relative layout so basically its not getting equally divided and distorted as well (distorted means some grid is not covering the full height)

please refer this:--
[issue with 6]6

[issue with 3]7
and so on

please help me out thanks in advance

答案1

得分: 1

为了使动态视图看起来像您所见,我编写了以下代码:

注意:第 5 个视图有一个例外,因为它需要包装在另一个视图内(在我的情况下是线性布局)。

当前,相对布局的宽度和高度如下固定:

android:layout_width="match_parent"
android:layout_height="200dp"

整个活动代码如下:

public class MainActivity2 extends AppCompatActivity {
    RelativeLayout relativeLayout;
    List<UserView> mUserViewList;
    List<Integer> mUidList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        relativeLayout = findViewById(R.id.relativeLayout);

        mUserViewList = new ArrayList<>();
        mUidList = new ArrayList<>();
        int size = 5;

        RelativeLayout.LayoutParams[] array = getgrid2(size);

        for (int i = 0; i < size; i++) {
            // 创建用户视图并添加到视图列表
            UserView userView = new UserView(this);
            mUserViewList.add(userView);

            // 设置 ID 并添加到 ID 列表
            userView.setId(i);
            mUidList.add(i);

            // 设置用户视图的文本
            userView.setTextView("User " + (i+1));

            if (array[i] == null)
                continue;
            // 设置布局
            mUserViewList.get(i).setLayoutParams(array[i]);
            // 将视图添加到布局中
            if (i == 4) {
                relativeLayout.addView(exceptionalMethodFor5view());
            }
            relativeLayout.addView(mUserViewList.get(i));
        }
    }

    // 其他方法和类的定义...

    private LinearLayout exceptionalMethodFor5view() {
        LinearLayout linearLayout = new LinearLayout(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setGravity(Gravity.CENTER);

        linearLayout.setLayoutParams(layoutParams);

        UserView newUserview1 = new UserView(this);
        newUserview1.setLayoutParams(new LinearLayout.LayoutParams(
                (Resources.getSystem().getDisplayMetrics().widthPixels) / 3,
                dpToPx(200) / 2));

        UserView newUserview2 = new UserView(this);
        newUserview2.setLayoutParams(new LinearLayout.LayoutParams(
                (Resources.getSystem().getDisplayMetrics().widthPixels) / 3,
                dpToPx(200) / 2));

        linearLayout.addView(newUserview1);
        linearLayout.addView(newUserview2);
        return linearLayout;
    }

    public static int dpToPx(float dp) {
        float density = Resources.getSystem().getDisplayMetrics().density;
        return Math.round(dp * density);
    }
}

这段代码涵盖了创建动态视图和设置其布局参数的过程。

英文:

遇到问题:在使用相对布局创建动态视图时。To make Dynamic view as you see it, I wrote the following code:

Note: There is an exception for the 5 view because it needs to be wrapped inside another view (in my case, the linear layout).

Currently, the width and height of the relative layout are fixed as follows:

    android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;200dp&quot;

whole activity code:

    public class MainActivity2 extends AppCompatActivity {
RelativeLayout relativeLayout;
List&lt;UserView&gt; mUserViewList;
List&lt;Integer&gt; mUidList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
relativeLayout = findViewById(R.id.relativeLayout);
mUserViewList = new ArrayList&lt;&gt;();
mUidList = new ArrayList&lt;&gt;();
int size = 5;
RelativeLayout.LayoutParams[] array = getgrid2(size);
for (int i = 0; i &lt; size; i++) {
//createing user view and addding in view list
UserView userView = new UserView(this);
mUserViewList.add(userView);
//setting id and addint to id list
userView.setId(i);
mUidList.add(i);
//settting text to user view
userView.setTextView(&quot;User &quot; + (i+1));
if (array[i]==null)
continue;
//setting layout
mUserViewList.get(i).setLayoutParams(array[i]);
//adding to views
if (i==4){
relativeLayout.addView(exptionalMethodFor5view());
}
relativeLayout.addView(mUserViewList.get(i));
}
}
private RelativeLayout.LayoutParams[] getgrid2(int size) {
//        int width = Math.max(relativeLayout.getMeasuredWidth(),relativeLayout.getMeasuredHeight());
//        int height = Math.min(relativeLayout.getMeasuredWidth(),relativeLayout.getMeasuredHeight());
int width = Resources.getSystem().getDisplayMetrics().widthPixels;
int height =dpToPx(200);
RelativeLayout.LayoutParams[] array =
new RelativeLayout.LayoutParams[size];
switch (size){
case 1: {
array[0] = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
break;
}
case 2:{
array[0] = new RelativeLayout.LayoutParams(
width/2,
RelativeLayout.LayoutParams.MATCH_PARENT );
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[0].addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
array[1] = new RelativeLayout.LayoutParams(
width/2,
ViewGroup.LayoutParams.MATCH_PARENT);
array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[1].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
break;
}
case 3: {
array[0] = new RelativeLayout.LayoutParams(
width/3,
RelativeLayout.LayoutParams.MATCH_PARENT );
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[0].addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
array[1] = new RelativeLayout.LayoutParams(
width/3,
ViewGroup.LayoutParams.MATCH_PARENT);
array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[1].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
array[2] = new RelativeLayout.LayoutParams(
width/3,
ViewGroup.LayoutParams.MATCH_PARENT);
array[2].addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
break;
}
case 4: {
array[0] = new RelativeLayout.LayoutParams(
width/2,
height/2 );
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[0].addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
array[1] = new RelativeLayout.LayoutParams(
width/2,
height/2);
array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[1].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
array[2] = new RelativeLayout.LayoutParams(
width/2,
height/2);
array[2].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[3] = new RelativeLayout.LayoutParams(
width/2,
height/2);
array[3].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[3].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
break;
}
case 5: {
//                array[0] = new RelativeLayout.LayoutParams(
//                        width/3,
//                        height/2 );
//                array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
//                array[0].addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
//                array[1] = new RelativeLayout.LayoutParams(
//                        width/3,
//                        height/2);
//top right
//                array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
//                array[1].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
array[2] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[2].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[3] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[3].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[3].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
array[4] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[4].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[4].addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
break;
}
case 6: {
array[0] = new RelativeLayout.LayoutParams(
width/3,
height/2 );
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[0].addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
array[1] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[1].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
array[2] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[2].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[3] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[3].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[3].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
array[4] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[4].addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
array[5] = new RelativeLayout.LayoutParams(
width/3,
height/2);
array[5].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
array[5].addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
break;
}
default:{
array[0] = new RelativeLayout.LayoutParams(
width/2,
ViewGroup.LayoutParams.MATCH_PARENT);
array[0].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[0].addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
array[1] = new RelativeLayout.LayoutParams(
width/2,
ViewGroup.LayoutParams.MATCH_PARENT);
array[1].addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
array[1].addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
break;
}
}
return array;
}
private LinearLayout exptionalMethodFor5view(){
LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setLayoutParams(layoutParams);
UserView newUserview1 = new UserView(this);
newUserview1.setLayoutParams(new LinearLayout.LayoutParams(
(Resources.getSystem().getDisplayMetrics().widthPixels)/3,
dpToPx(200)/2));
UserView newUserview2 = new UserView(this);
newUserview2.setLayoutParams(new LinearLayout.LayoutParams(
(Resources.getSystem().getDisplayMetrics().widthPixels)/3,
dpToPx(200)/2));
linearLayout.addView(newUserview1);
linearLayout.addView(newUserview2);
return linearLayout;
}
public static int dpToPx(float dp) {
float density = Resources.getSystem().getDisplayMetrics().density;
return Math.round(dp * density);
}
}

huangapple
  • 本文由 发表于 2023年6月15日 17:07:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定