简体   繁体   English

Android Studio-以编程方式处理布局/视图的问题

[英]Android Studio - issue with manipulating Layouts/Views programmatically

I have a few issues with setting LayoutParams and other parameters of my layouts/views programmatically. 我以编程方式设置LayoutParams和布局/视图的其他参数时遇到一些问题。 I cannot specify these in a XML layout file because whether they appear depends on the data held in the database. 我无法在XML布局文件中指定它们,因为它们是否出现取决于数据库中保存的数据。

The following is a function I use to create a new "Section" which consists of a FrameLayout with its children being View and TextView: 以下是我用来创建新的“ Section”的功能,该功能由一个FrameLayout组成,其子级为View和TextView:

public FrameLayout createSection(long id, String name) {
    FrameLayout frame = new FrameLayout(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
    params.setMargins(15, 15, 15, 15);
    frame.setLayoutParams(params);

    View view = new View(this);
    LayoutParams viewParams = new LayoutParams(LayoutParams.MATCH_PARENT, 100);
    view.setLayoutParams(viewParams);
    view.setId(toIntExact(id));
    view.setBackgroundResource(R.color.colorButton);
    frame.addView(view);

    TextView text = new TextView(this);
    LayoutParams textParams = new LayoutParams(LayoutParams.MATCH_PARENT, 100);
    textParams.setMarginStart(15);
    text.setGravity(Gravity.CENTER);
    text.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_START);
    text.setTextColor(getResources().getColor(R.color.colorTextSecondary));
    text.setText(name);
    frame.addView(text);

    return frame;
}

The parent of this newly created FrameLayout is LinearLayout and so based on the other similar questions on StackOverflow I figured setting parameters for FrameLayout should be done through LinearLayout.LayoutParams. 这个新创建的FrameLayout的父级是LinearLayout,因此基于StackOverflow上的其他类似问题,我认为应该通过LinearLayout.LayoutParams完成FrameLayout的设置参数。 However, this does not make a change. 但是,这并没有改变。 The initial XML page contains this: Initial XML page 初始XML页面包含以下内容: 初始XML页面

The first "SECTION" is created in the XML file, and the other two are created through 'createSection' function. 第一个“ SECTION”在XML文件中创建,另外两个通过“ createSection”函数创建。 This is the outcome: Design outcome 这就是结果: 设计结果

The issue is that the margins are not set properly and the TextView doesn't seem to care about the Gravity + TextAlignment combination that I'm using. 问题是页边距设置不正确,TextView似乎并不关心我使用的Gravity + TextAlignment组合。

I would appreciate any help that I could get to resolve this issue. 如果能解决此问题,我将不胜感激。

I apologise for wasting anyone's time. 对于浪费任何人的时间,我深表歉意。 The code seems to work and the margin sizes are different due to these being set in terms of pixels (px) rather than dp as it is in the XML file. 该代码似乎可以正常工作,并且页边距大小不同,这是因为这些像素是根据像素(px)而不是XML文件中的dp设置的。

I also forgot to add text.setLayoutParams(textParams); 我也忘记添加text.setLayoutParams(textParams); to the TextView object. 到TextView对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM