Android 以编程方式将属性AlignParentRight 设置为RelativeLayout 中的按钮不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12509394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:51:12  来源:igfitidea点击:

Programmatically set attribute AlignParentRight to button in RelativeLayout is not working?

androidandroid-layoutandroid-buttonandroid-relativelayout

提问by Mokkapps

I try to programmatically set the attribute AlignParentRight to button in RelativeLayout but the program crashes with a NullPointerException. Here my code:

我尝试以编程方式将 AlignParentRight 属性设置为 RelativeLayout 中的按钮,但程序因 NullPointerException 而崩溃。这是我的代码:

//add horizontal realative layout
RelativeLayout layouth = new RelativeLayout(this);
layouth.setLayoutParams(new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

//add textview for label
TextView t = new TextView(this);
t.setText(d.getName());
t.setTextSize(25);
t.setId(2);      
t.setLayoutParams(new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

//add button
Button b = new Button(this);
b.setText(d.getName());
RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)b.getLayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
layoutParams.addRule(RelativeLayout.LEFT_OF, 2);
b.setLayoutParams(layoutParams);   

//add textview and button to horizontal linear layout
layouth.addView(t);
layouth.addView(b);
//add horizontal linear layout to vertical linear layout
layoutv = (LinearLayout) findViewById(R.id.ProjectViewLinearID);
layoutv.addView(layouth);

Where is my problem? The error occurs in the program line layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

我的问题在哪里?错误发生在程序行layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

UPDATE WITH SOLUTION:

更新解决方案:

This code works for me:

这段代码对我有用:

// Creating a new RelativeLayout
RelativeLayout layouth = new RelativeLayout(this);

// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.MATCH_PARENT);

// Creating a new TextView
TextView tv = new TextView(this);
tv.setText(d.getName());
tv.setTextSize(25);

//add button
Button b = new Button(this);
b.setText(d.getName());

// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
         RelativeLayout.LayoutParams.WRAP_CONTENT,
         RelativeLayout.LayoutParams.WRAP_CONTENT);
         lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

// Setting the parameters on the TextView
tv.setLayoutParams(lp);
b.setLayoutParams(lp);

// Adding the TextView to the RelativeLayout as a child
layouth.addView(tv);
layouth.addView(b);

采纳答案by njzk2

When you call b.getLayoutParams(), b is not in a layout. therefore, it does not have a layoutparams (plus, it wouldn't know that you are expecting a RelativeLayout.LayoutParams at this point).

当您调用 时b.getLayoutParams(), b 不在布局中。因此,它没有 layoutparams(此外,它不会知道此时您正在期待一个 RelativeLayout.LayoutParams)。

You need to create a new LayoutParams instance, like you did for the TextView.

您需要创建一个新的 LayoutParams 实例,就像您为 TextView 所做的那样。

回答by Mukesh Soni

replace

代替

RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)b.getLayoutParams();

with

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);