简体   繁体   English

如何以编程方式将相对布局中的视图与最左,最中和最右对齐?

[英]How to align views in a relative layout to far left, center, and far right programmatically?

I have a relative layout that has two buttons and one textview. 我有一个相对的布局,具有两个按钮和一个textview。 What i'm trying to have happen is having one button on the far left, the textview in the center, and the other button on the far right. 我想发生的事情是在最左侧有一个按钮,在中央有textview,在最右侧有另一个按钮。 Trying to do this without XML. 尝试在没有XML的情况下执行此操作。

Here's my code: 这是我的代码:

        RelativeLayout fm = new RelativeLayout(this);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        fm.setLayoutParams(lp);
        fm.setBackgroundColor(Color.CYAN);

        Button done = new Button(this);
        done.setId(10);
        done.setText("Done");

        Button save = new Button(this);
        save.setId(12);
        save.setText("Save");

        TextView formManager = new TextView(this);
        formManager.setId(11);
        formManager.setText("Form Manager");

        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        fm.addView(formManager, lp);

        lp.removeRule(RelativeLayout.CENTER_IN_PARENT);

        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        fm.addView(done, lp);

        lp.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);

        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        fm.addView(save, lp);

        lp.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        mainLayout.addView(fm);

Problem is...is that the Save button stretches and takes up the whole layout along with being very thin. 问题是...保存按钮会拉伸并占据整个布局,而且非常细。 Basically with this code nothing is happening like I thought. 基本上,使用此代码不会像我想的那样发生任何事情。 Any ideas on how to achieve this goal? 关于如何实现这一目标的任何想法?

try this way 尝试这种方式

  LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
    RelativeLayout fm = new RelativeLayout(this);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    fm.setLayoutParams(lp);
    fm.setBackgroundColor(Color.CYAN);

    Button done = new Button(this);
    done.setId(10);
    done.setText("Done");

    Button save = new Button(this);
    save.setId(12);
    save.setText("Save");

    TextView formManager = new TextView(this);
    formManager.setId(11);
    formManager.setText("Form Manager");

    RelativeLayout.LayoutParams lpp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    formManager.setLayoutParams(lpp);
    lpp.addRule(RelativeLayout.CENTER_IN_PARENT);
    fm.addView(formManager, lpp);

    RelativeLayout.LayoutParams doneLayoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    done.setLayoutParams(doneLayoutParams);
    doneLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    fm.addView(done, doneLayoutParams);

    RelativeLayout.LayoutParams saveLayoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    save.setLayoutParams(saveLayoutParams);
    saveLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    fm.addView(save, saveLayoutParams);


    mainLayout.addView(fm);

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

相关问题 如何以编程方式在相对布局中设置TextViews的align父权属性 - How to set the align parent right attribute of TextViews in relative layout programmatically 如何对齐始终位于 window 最右侧的 JTextField? - How to align a JTextField that it is always on the far right side of the window? 在Gravity.CENTER相对布局中向左对齐膨胀视图 - Align inflated view left in Gravity.CENTER Relative Layout 如何以编程方式在相对布局中对齐按钮? - How to align a button in Relative Layout Programmatically? Android以编程方式设置布局左对齐和垂直居中 - Android set Layout align left and center vertical programmatically 如何在HBox左,中,右对齐儿童 - How to align children in a HBox Left, Center and Right 如何以编程方式设置对齐(右或左)TextView? - How to set align (right or left) programmatically TextView? 关于javaFX,需要将列表视图中的节点对齐到最右边 - About javaFX, Need to align a node that is inside a listview to the far right 如何设置要在Java中最右侧显示的图标 - How to Set Icon to display on the far right in Java JMenuBar中的组件要在JSeparator之后对齐吗? - Component inside JMenuBar wants to align far right after JSeparator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM